Created
March 5, 2019 07:03
-
-
Save faaezahmd/957b4e2764f3431d5f75670a2294d2da to your computer and use it in GitHub Desktop.
A function that copies content from a div to clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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