Created
May 13, 2019 12:55
-
-
Save nirlanka/21dc4c1b2d0432eb7976801072758d64 to your computer and use it in GitHub Desktop.
Click to copy text (from table cell) in HTML with pure Javascript
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
const copyToClipboard = str => { | |
const el = document.createElement('textarea'); | |
el.value = str; | |
document.body.appendChild(el); | |
el.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(el); | |
}; | |
document.addEventListener('click', (e) => { | |
if(e.target && e.target.nodeName== 'TD') { | |
copyToClipboard(e.target.innerHTML); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment