Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Copy variable contents to the user clipboard
copyText (textToCopy) {
this.copied = false
// Create textarea element
const textarea = document.createElement('textarea')
// Set the value of the text
textarea.value = textToCopy
// Make sure we cant change the text of the textarea
textarea.setAttribute('readonly', '');
// Hide the textarea off the screnn = 'absolute'; = '-9999px';
// Add the textarea to the page
// Copy the textarea
try {
var successful = document.execCommand('copy');
this.copied = true
} catch(err) {
this.copied = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment