Skip to content

Instantly share code, notes, and snippets.

@oliverstech
Last active September 15, 2021 17:48
Show Gist options
  • Save oliverstech/2ff45355bf2e6824f1b5c12d7a420eaf to your computer and use it in GitHub Desktop.
Save oliverstech/2ff45355bf2e6824f1b5c12d7a420eaf to your computer and use it in GitHub Desktop.
How to copy text in HTML with JS
<html>
<h1>Example</h1>
<p>Demo HTML from: https://git.io/JuA3B </p>
<input readonly="true" id="field" placeholder="Press 'Go' to begin.">
<button onclick="go()">Go!</button>
<p id="endmsg"></p>
<script>
function go() {
document.getElementById("field").value = "Desired text to copy";
document.getElementById("field").select();
document.execCommand('copy');
document.getElementById('endmsg').innerHTML = "Paste your clipboard now!";
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment