Sometimes, we want to copy or put text on the clipboard with JavaScript.
In this article, we’ll look at how to copy or put text on the clipboard with JavaScript.
How to copy or put text on the clipboard with JavaScript?
To copy or put text on the clipboard with JavaScript, we can use the document.execCommand
method.
For instance, we write
const copy = () => {
document.getElementById("myText").select();
document.execCommand("copy");
};
to create copy
function that selects the input element with ID myText
with getElementById
.
Then we call select
on it to select the input text.
Next we call document.execCommand
with 'copy'
to copy the selected text into the clipboard.
Conclusion
To copy or put text on the clipboard with JavaScript, we can use the document.execCommand
method.