![](/img/Ben-cartoon-150px.png)
Copy text to the clipboard with JavaScript
![Learn JavaScript Copy text to the clipboard with JavaScript](/music/images/javascript.jpg)
See the working demo below.
Demo: How to copy text to the clipboard with JavaScript
JavaScript
This is pretty much the standard JavaScript you'll paste into your page script block - or in a .js file if you prefer.
function copyTextToClipboard() {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied this text to the clipboard: " + copyText.value);
}
CSS
Not necessary delete/edit as required.
body{
font-size:2em;
font-weight:bold;
}
input{
font-size:50px;
}
button{
font-size:50px;
}
Conclusion
A few lines of JavaScript is all you need to copy text to the clipboard with javascript