Created
September 25, 2020 15:42
-
-
Save n8jadams/e5b2405882b12a9aa48281bb8a8c165f to your computer and use it in GitHub Desktop.
Simple function to copy some text to the clipboard in the browser
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(textToCopy: string): void { | |
const tmpInputEl = document.createElement('input') | |
tmpInputEl.value = textToCopy | |
tmpInputEl.type = 'text' | |
tmpInputEl.style.position = 'absolute' | |
tmpInputEl.style.left = '9000vw' | |
document.body.appendChild(tmpInputEl) | |
tmpInputEl.select() | |
document.execCommand('copy') | |
document.body.removeChild(tmpInputEl) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment