Javascript Web Technologies

Copy to Clipboard – Javascript

<!DOCTYPE html>
<html>
<body>

<p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>

<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>

<p>The document.execCommand() method is not supported in IE8 and earlier.</p>

<script>
function myFunction() {
  var copyText = document.getElementById("myInput");
  copyText.select();  //This is to select the text that will be copyied
  copyText.setSelectionRange(0, 99999)
  document.execCommand("copy"); // This command will copy the selected text to the clipboard
  alert("Copied the text: " + copyText.value);
}
</script>

</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *