Last active
August 19, 2022 20:56
-
-
Save n8jadams/b38f508975424822d5df63f0d3646b4e to your computer and use it in GitHub Desktop.
Copy some text to the clipboard
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
// 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