Skip to content

Instantly share code, notes, and snippets.

@pomeh
Created July 27, 2014 17:39
Show Gist options
  • Save pomeh/5523a71d2c67b32800d0 to your computer and use it in GitHub Desktop.
Save pomeh/5523a71d2c67b32800d0 to your computer and use it in GitHub Desktop.
JavaScript copy to clipboard function
function copyToClipboard(value) {
var tbx = document.createElement('input')
document.body.appendChild(tbx);
tbx.value = value;
tbx.focus();
tbx.setSelectionRange(0, tbx.value.length);
document.execCommand("copy");
document.body.removeChild(tbx);
}
// usage:
// I tested it only with node-webkit
copyToClipboard('someValue');
// ressources used
// https://groups.google.com/forum/#!topic/node-webkit/1juFqJVPZRQ
// https://github.com/b1rdex/nw-contextmenu/blob/master/index.js
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement.setSelectionRange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment