Skip to content

Instantly share code, notes, and snippets.

@VassilisPallas
Created January 19, 2017 18:10
Show Gist options
  • Save VassilisPallas/ace217a0cf506e408cc9f90e129c8088 to your computer and use it in GitHub Desktop.
Save VassilisPallas/ace217a0cf506e408cc9f90e129c8088 to your computer and use it in GitHub Desktop.
simple fuction that copies a text to clipboard.
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