Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created July 27, 2017 19:11
Show Gist options
  • Save balinterdi/72a678b42f9fc4d6258734c350e2631d to your computer and use it in GitHub Desktop.
Save balinterdi/72a678b42f9fc4d6258734c350e2631d to your computer and use it in GitHub Desktop.
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