-2

I'm using the following function to copy to clipboard.

function CopyToClipboard(containerid) {
if (document.selection) { 
    var range = document.body.createTextRange();
    range.moveToElementText(document.getElementById(containerid));
    range.select().createTextRange();
    document.execCommand("copy"); 

} else if (window.getSelection) {
    var range = document.createRange();
     range.selectNode(document.getElementById(containerid));
     window.getSelection().addRange(range);
     document.execCommand("copy");
     swal("Signature copied!","Successfully copied to clipboard.", "success");
}}

I'm updating links using an input form (js example shown below). The copy function doesn't work when the links get updated.

$('#fbInput').on('input',function(e){
  var fbInput = $('#fbInput').val();
  textFB.attr("href", fbInput);
  if(fbInput.length > 5 && fbInput.includes("http") !== true) { swal("Include HTTPS.","Start your URL with https://", "warning"); }
});

Please advise.

Tanzir Rahman
  • 165
  • 1
  • 2
  • 19
  • 1
    "doesn't work" *how*? What indication of a problem is there? Is it not invoked at all? What's supposed to be invoking it? If it is invoked, when you step through in the debugger, how and where does it fail? – David Jan 19 '18 at 15:09
  • Did you check your browser's console for errors? –  Jan 19 '18 at 15:09
  • So you assign an event handler to a link, then do something that modifies the link and then the event handler no longer works - Is that correct? – Reinstate Monica Cellio Jan 19 '18 at 15:11
  • My guess is that the OP is probably saying when they copy the text, the attribute they set does not appear in the copied code, instead it is the original. – epascarello Jan 19 '18 at 15:14
  • Console doesn't show any errors. I'm just trying to update links using a form and trying to copy to clipboard. – Tanzir Rahman Jan 19 '18 at 15:17
  • No, nothing gets copied when I update. – Tanzir Rahman Jan 19 '18 at 15:18
  • Okay, when I manually select it and use the copy button. This error shows. index.js:86 [Deprecation] The behavior that Selection.addRange() merges existing Range and the specified Range was removed. See https://www.chromestatus.com/features/6680566019653632 for more details. CopyToClipboard @ index.js:86 onclick @ index.html:146 – Tanzir Rahman Jan 19 '18 at 15:21

1 Answers1

0

This is the original thread I used the copy to clipboard function from: How to copy text from a div to clipboard

Someone commented it won't work if something is already copied to the clipboard. I'm still trying to figure out why. Then I tried to figure out how to clear the clipboard and the answer is: browsers don't allow it due to security risks.

Tanzir Rahman
  • 165
  • 1
  • 2
  • 19