Create 'Automatic copy to clipboard' javascript

I want a page to copy some preset text to the clipboard for a user on load.

function myFunction() {
  var copyText = document.getElementById("myInput");;
  copyText.setSelectionRange(0, 99999)
  alert("Copied the text: " + copyText.value);

function myClick() {
<body onload="myClick()">
    <input type="text" value="COPIED" id="myInput">
    <button onclick="myFunction()" id="myButton">Copy text</button>

The alert appears and the console logs the value – COPIED

But the text isn’t actually copied to the clipboard. Clicking on the button ‘Copy text’ – does work?

12 thoughts on “Create 'Automatic copy to clipboard' javascript”

Leave a Comment