Last active
January 4, 2016 15:20
-
-
Save fidlerryan/09f8121594674508c86e to your computer and use it in GitHub Desktop.
Copy to Clipboard from input
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
document.addEventListener('DOMContentLoaded', function() { | |
var copybtn = document.getElementById('copybtn'); | |
var authcode = document.getElementById('authcode'); | |
copybtn.addEventListener('click', function() { | |
// select the authorization code value | |
authcode.setSelectionRange(0, authcode.value.length); | |
try{ | |
// with text selected, execute the copy command | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
if (msg == 'successful') copybtn.value = 'Copied!'; | |
console.log('Copy authorization code command was ' + msg); | |
} catch(err) { | |
console.log('Oops, unable to copy'); | |
} | |
// remove the selection | |
window.getSelection().removeAllRanges(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment