Created
July 27, 2017 19:11
-
-
Save balinterdi/72a678b42f9fc4d6258734c350e2631d to your computer and use it in GitHub Desktop.
Copy some text to the clipboard using the brower's DOM API
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
export default function copyTextToClipboard(text) { | |
let textArea = document.createElement("textarea"); | |
textArea.value = text; | |
document.body.appendChild(textArea); | |
textArea.select(); | |
try { | |
// Now that we've selected the anchor text, execute the copy command | |
let successful = document.execCommand('copy'); | |
let message = successful ? 'successful' : 'unsuccessful'; | |
console.log(`Copy email command was ${message}`); | |
} catch(error) { | |
console.error(`Oops, unable to copy: ${error}`); | |
} | |
document.body.removeChild(textArea); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment