Created
January 19, 2017 18:10
-
-
Save VassilisPallas/ace217a0cf506e408cc9f90e129c8088 to your computer and use it in GitHub Desktop.
simple fuction that copies a text to clipboard.
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
jQuery('#copy').on('click', function () { | |
copyToClipboard(); | |
}); | |
function copyToClipboard() { | |
var target = jQuery('#hidden_text'); | |
// make it visible, so can be focused | |
target.attr('type', 'text'); | |
target.focus(); | |
// select all the text | |
target[0].setSelectionRange(0, target.val().length); | |
// copy the selection | |
var succeed; | |
try { | |
succeed = document.execCommand("copy"); | |
} catch (e) { | |
succeed = false; | |
} | |
// hide input again | |
target.attr('type', 'hidden'); | |
return succeed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment