js copy text to system clipboard

JavaScript
[Edit]
+
0
-
0

js copy text to system clipboard

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Hint: this solution works in older browsers. // Warning: document.execCommand() function is marked as depricated. window.Clipboard = new function() { var transit = null; var element = null; function copy(text) { element.value = text; element.style.display = 'block'; element.select(); document.execCommand('copy'); element.style.display = 'none'; } window.addEventListener('load', function() { element = document.createElement('textarea'); element.setAttribute('style', 'border: none; width: 0; height: 0; display: none'); document.body.appendChild(element); if (transit) { copy(transit); } }); this.copy = function(text) { if (element) { if (element.parentNode == null) { throw new Error('Element has been removed from document.'); } copy(text); } else { transit = text; } }; }; // Usage example: Clipboard.copy('Hello Dirask!')
[Edit]
+
0
-
0

js copy text to system clipboard

1 2 3 4 5 6 7 8 9
// Warning: document.execCommand() function is marked as depricated. var input = document.querySelector('#input-element'); input.value = 'Text to copy to clipboard ...'; // replace the text to own one input.select(); // it is required to select inpout text before copy operation document.execCommand('copy'); // copies selected text into the system clipboard