copy to clipboard by given string javascript code example
Example 1: copy to clipboard javascript
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
Example 2: javascript copy to clipboard
var copyTextarea = document.getElementById("someTextAreaToCopy");
copyTextarea.select();
document.execCommand("copy");