Skip to content

Instantly share code, notes, and snippets.

@timwright12
Last active September 19, 2016 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timwright12/645f7153c581bee4c7a3 to your computer and use it in GitHub Desktop.
Save timwright12/645f7153c581bee4c7a3 to your computer and use it in GitHub Desktop.
<!--
Via: http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
-->
<textarea class="js-copytextarea">Hello I'm some text</textarea>
<button class="js-textareacopybtn">Copy Text</button>
<script>
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
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');
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment