Last active
September 19, 2016 01:25
-
-
Save timwright12/645f7153c581bee4c7a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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