copy to clipboard javascript

JavaScript
function copy() {
  var copyText = document.querySelector("#input");
  copyText.select();
  document.execCommand("copy");
}

document.querySelector("#copy").addEventListener("click", copy);function copyToClipboard(text) {
   const elem = document.createElement('textarea');
   elem.value = text;
   document.body.appendChild(elem);
   elem.select();
   document.execCommand('copy');
   document.body.removeChild(elem);
}  function myFunction() {
  /* Get the text field */
  var copyText = document.getElementById("myInput");

  
  /* Select the text field */
  
  copyText.select(); 
  
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand("copy");

  
  /* Alert the copied text */
  
  alert("Copied the text: " + copyText.value);
} // Since Async Clipboard API is not supported for all browser!
function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");
  textArea.value = text
  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }

  document.body.removeChild(textArea);
}function copyToClipboard(text) {
  var input = document.body.appendChild(document.createElement("input"));
  input.value = text;
  input.focus();
  input.select();
  document.execCommand('copy');
  input.parentNode.removeChild(input);
}var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard
Source

Also in JavaScript: