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.  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>
function copyClipboard() {
  var elm = document.getElementById("divClipboard");
  // for Internet Explorer

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

    var selection = window.getSelection();
    var range = document.createRange();
    alert("Copied div content to clipboard");
   <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.
   <button onclick="copyClipboard()">Copy text</button>