Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Created June 20, 2019 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchumahara/695d09e02ee892f3594b4ba7b2a66397 to your computer and use it in GitHub Desktop.
Save manchumahara/695d09e02ee892f3594b4ba7b2a66397 to your computer and use it in GitHub Desktop.
function copyText( text ){
var div = document.createElement( 'div' );
div.innerHTML = text;
div.style.height = '';
div.style.position = 'fixed';
div.style.bottom = '0';
div.style.left = '0';
div.style.opacity = '0';
div.style.display = 'block';
div.style.overflow = 'hidden';
div.style.zIndex = 9999999999;
document.body.appendChild( div );
var range = document.createRange();
range.selectNode(div);
window.getSelection().addRange(range);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
var successful = false;
try {
successful = document.execCommand('copy');
} catch(err) {
}
window.getSelection().removeAllRanges();
div.remove();
return successful;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment