copy specific text to clipboard javascript code example
Example 1: javascript text to clipboard
function copyToClipboard(text) {
const elem = document.createElement('textarea');
elem.value = text;
document.body.appendChild(elem);
elem.select();
document.execCommand('copy');
document.body.removeChild(elem);
}
Example 2: javascript copy to clipboard
<head><script>
function copyToCliBoard() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
</script></head>
<body>
<input type="text" value="Hello World" id="myInput">
<button onclick="copyToCliBoard()">Copy text</button>
</body>
Example 3: javascript copy to clipboard
$(document).ready(function(){
$("#ewefmwefmp").click(function(){
//alert()
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
});
});
Example 4: javascript copy to clipboard
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select(); //select the text area
document.execCommand("copy"); //copy to clipboard