How to copy the div content to Clipboard

Copying the text from the text, textarea field to clipboard is easy, we can use the select() method to the input or textarea element.
element.select()  click here to learn from w3school for copying text from text input.

To learn how to copy the content from div to the clipboard, we have to use javascript to support for all browser.

<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
   <div class="code-bg" id="divClipboard">
      Click on the button to copy the text from the div element. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.
   </div>
   <button onclick="copyClipboard()">Copy text</button>
</body>
</html>