Copy to Clipboard
by TigerAsks
HTML
<button onclick="pushToClipboard()">Copy 'Hello World'</button>
JavaScript
function pushToClipboard(){
var textArea = document.createElement("textarea");
textArea.value = 'Hello World';
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
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.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}