Skip to content

Instantly share code, notes, and snippets.

@n8jadams
Last active August 19, 2022 20:56
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/b38f508975424822d5df63f0d3646b4e to your computer and use it in GitHub Desktop.
Save n8jadams/b38f508975424822d5df63f0d3646b4e to your computer and use it in GitHub Desktop.
Copy some text to the clipboard
// Newer and preferred way
function copyToClipboard(text) {
navigator.clipboard.writeText(text)
}
// Support on old browsers
function copyToClipboard(text) {
const tmpInput = document.createElement('input')
tmpInput.value = text
tmpInput.type = 'text'
tmpInput.style.position = 'absolute'
tmpInput.style.left = '9000vw'
document.body.appendChild(tmpInput)
tmpInput.select()
document.execCommand('copy')
document.body.removeChild(tmpInput)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment