Last active
September 15, 2021 17:48
-
-
Save oliverstech/2ff45355bf2e6824f1b5c12d7a420eaf to your computer and use it in GitHub Desktop.
How to copy text in HTML with JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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