Copy to Clipboard, Pure JS

by Matthew Schenker

HTML

<input type="text" value="http://www.nytimes/.com?story=now" id="CopyInput">
<button onclick="myFunction()">Copy text</button>
<p><input type="text" placeholder="Paste here for test" /></p>

JavaScript

function myFunction() {
  var copyText = document.getElementById("CopyInput");
  copyText.select();
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy");
  alert("URL Copied: " + copyText.value);
}