Skip to content

Instantly share code, notes, and snippets.

@tgmarinho
Created May 19, 2018 00:28
Show Gist options
  • Save tgmarinho/4807048aad6e0431393d2569c862c237 to your computer and use it in GitHub Desktop.
Save tgmarinho/4807048aad6e0431393d2569c862c237 to your computer and use it in GitHub Desktop.
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.focus();
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
});
<p>
<button class="js-textareacopybtn" style="vertical-align:top;">Copy Textarea</button>
<textarea class="js-copytextarea">Hello I'm some text</textarea>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment