Skip to content

Instantly share code, notes, and snippets.

@n8jadams
Created September 25, 2020 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8jadams/e5b2405882b12a9aa48281bb8a8c165f to your computer and use it in GitHub Desktop.
Save n8jadams/e5b2405882b12a9aa48281bb8a8c165f to your computer and use it in GitHub Desktop.
Simple function to copy some text to the clipboard in the browser
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