Created
July 27, 2014 17:39
-
-
Save pomeh/5523a71d2c67b32800d0 to your computer and use it in GitHub Desktop.
JavaScript copy to clipboard function
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
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