Created
January 9, 2019 20:32
-
-
Save metodribic/75647d5083f0ff1de0eee7c7c1016be7 to your computer and use it in GitHub Desktop.
Copy text to clipboard 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
function copy(text) { | |
const textarea = document.createElement("textarea"); | |
textarea.style.opacity = '0'; | |
textarea.style.position = 'fixed'; | |
textarea.textContent = text; | |
const body = document.getElementsByTagName('body')[0]; | |
body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand('copy'); | |
body.removeChild(textarea); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment