Skip to content

Instantly share code, notes, and snippets.

@faaezahmd
Created March 5, 2019 07:03
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 faaezahmd/957b4e2764f3431d5f75670a2294d2da to your computer and use it in GitHub Desktop.
Save faaezahmd/957b4e2764f3431d5f75670a2294d2da to your computer and use it in GitHub Desktop.
A function that copies content from a div to clipboard
// copy content from a div
function copyClipboard(query) {
var elm = document.querySelector(query);
// for Internet Explorer
if(document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(elm);
range.select(); // select content before copying
document.execCommand("Copy");
document.selection.empty(); // unselect content
}
else if(window.getSelection) {
// other browsers
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(elm);
selection.removeAllRanges();
selection.addRange(range); // select content before copying
document.execCommand("Copy");
selection.removeAllRanges(); // unselect content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment