0

I want to copy text in any span section and use it anywhere i want on CTRL+V. but below code works only with input and not with span.

Code is as per below:

  $scope.copyCode = function(name, code, index) {
  $("#buttonCopyClass" + index).css("display", "none");
  $("#buttonCopiedClass" + index).css("display", "block");
  ga('send', 'event', 'userCopiedCoupen', name, code);
  copyToClipboard(document.getElementById("copyTarget"));
  return event.stopPropagation();
};
copyToClipboard = function(elem) {
  var currentFocus, e, isInput, origSelectionEnd, origSelectionStart, succeed, target, targetId;
  targetId = '_hiddenCopyText_';
  isInput = elem.tagName === 'INPUT' || elem.tagName === 'TEXTAREA';
  origSelectionStart = void 0;
  origSelectionEnd = void 0;
  if (isInput) {
    console.log(78);
    target = elem;
    origSelectionStart = elem.selectionStart;
    origSelectionEnd = elem.selectionEnd;
  } else {
    console.log(7889);
    target = document.getElementById(targetId);
    if (!target) {
      target = document.createElement('textarea');
      target.style.position = 'absolute';
      target.style.left = '-9999px';
      target.style.top = '0';
      target.id = targetId;
      document.body.appendChild(target);
    }
    target.textContent = elem.textContent;
  }
  currentFocus = document.activeElement;
  target.focus();
  target.setSelectionRange(0, target.value.length);
  succeed = void 0;
  try {
    succeed = document.execCommand('copy');
  } catch (error1) {
    e = error1;
    succeed = false;
  }
  if (currentFocus && typeof currentFocus.focus === 'function') {
    currentFocus.focus();
  }
  if (isInput) {
    elem.setSelectionRange(origSelectionStart, origSelectionEnd);
  } else {
    target.textContent = '';
  }
  return succeed;
};

The above code i have used to fetch the text of a input feild but i need this to work with a sapn feild.

caffeinated.tech
  • 5,846
  • 19
  • 36
Raaj Dubey
  • 172
  • 1
  • 12
  • somebody might help if you can please put it to jsfiddle or jsbin ? – krish Mar 06 '17 at 15:19
  • 1
    There are some related questions like http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript that might give you some pointers. – JohanSellberg Mar 06 '17 at 15:24

0 Answers0