Last active
February 6, 2022 14:29
-
-
Save MWarCZ/4fc759339cc04366419b9fbe6546686a to your computer and use it in GitHub Desktop.
Copy content of element to clipboard.
This file contains hidden or 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
function copyToClipboard(elWitchText) { | |
if(navigator.clipboard) { | |
// For newer web browser | |
var value = elWitchText.value ? elWitchText.value : elWitchText.innerText; | |
return navigator.clipboard.writeText(value) | |
} | |
else { | |
// For older web browser (<2020) | |
var tmp = document.createElement('textarea') | |
tmp.value = elWitchText.value ? elWitchText.value : elWitchText.innerText; | |
document.body.append(tmp); | |
tmp.select(); | |
document.execCommand("copy"); | |
document.body.removeChild(tmp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment