0
<script>
function copyClipboard() {
  var elm = document.getElementById("divClipboard");
  // for Internet Explorer

  if(document.body.createTextRange) {
    var range = document.body.createTextRange();
    range.moveToElementText(elm);
    range.select();
    document.execCommand("Copy");
    alert("Copied div content to clipboard");
  }
  else if(window.getSelection) {
    // other browsers

    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(elm);
    selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand("Copy");
    alert("Copied div content to clipboard");
  }
}
</script>

Plese modify this code so that it can be used for classes rather then for id. If I change "getElementById" to "getElementsByClassName" the code doesn't work. So please help .

1 Answers1

0

document.getElementsByClassName returns an array of elements where all the elements have the same class. So if there's only one element of class "divClipboard", you can change that line to,

var elm = document.getElementsByClassName("divClipboard")[0];
Ron
  • 99
  • 2