Created
April 7, 2019 19:02
-
-
Save kudinovfedor/f3c02ce85ac919a1ee497a7231dd4d5f to your computer and use it in GitHub Desktop.
Copy string to clipboard (buffer)
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 string to the clipboard | |
* | |
* @example | |
* copy('random string'); | |
* | |
* @param {string} string | |
* @returns {void} | |
*/ | |
const copy = (string) => { | |
const textarea = document.createElement('textarea'); | |
textarea.value = string; | |
document.body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(textarea); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment