Skip to content

Instantly share code, notes, and snippets.

Embed
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;
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
You can’t perform that action at this time.