Created
December 3, 2020 06:45
-
-
Save justforuse/9940f6328235adbc43c310ec9993666a to your computer and use it in GitHub Desktop.
copy string to your clipboard by javascript
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(str) { | |
if (!str) { | |
return; | |
} | |
// input element cannot keep '\n', so you can turn to textarea | |
const inputEl = document.createElement('textarea'); | |
inputEl.value = str; | |
inputEl.style.cssText = 'position: absolute; top: -9999px; left: -9999px;'; | |
document.body.appendChild(inputEl); | |
inputEl.select(); | |
inputEl.setSelectionRange(0, 99999); | |
document.execCommand('copy'); | |
document.body.removeChild(inputEl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment