Copy Button

Copies the button contents to the clipboard. Issues getting it working.

<html> Copy this Text to Clipboard <br><br> <input placeholder=“Paste here, to try!”> </html>

<html> <script> function copyById(containerId) {

var range_ = document.createRange(); // create new Range object
range_.selectNode(document.getElementById(containerId)); // set our Range to contain the Node we want to copy from
window.getSelection().removeAllRanges(); // remove any previous selections
window.getSelection().addRange(range_); // select
document.execCommand("copy"); // copy to clipboard
window.getSelection().removeAllRanges(); // remove selection

}

add onClick event handler to your button with additional function() to not invoke copyById immediately: document.getElementById('copy-button').onclick = function() { copyById('copy-button'); } </script> </html>

  • Last modified: 2021/07/09 15:48
  • by big_josh_energy