Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Copy some text to the clipboard using the brower's DOM API
export default function copyTextToClipboard(text) {
let textArea = document.createElement("textarea");
textArea.value = text;
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}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment