window.ENTITIES={'/api/snippets/javascript/js%20copy%20text%20to%20system%20clipboard':[{"result":true,"message":null,"batch":{"type":"javascript","name":"js copy text to system clipboard","items":[{"id":"DZW2mp","type":"javascript","name":"js copy text to system clipboard","content":"navigator.clipboard.writeText('Hello Dirask!');\n\n// WARNING: IT WORKS ONLY IN MODERN BROWSERS !!!","source":"https://dirask.com/posts/JavaScript-how-to-copy-text-to-system-clipboard-Kj87Ap","author":{"id":"JDKm8o","name":"GamerGoals22","avatar":"1629059482346__JDKm8o__w40px_h40px.jpg","points":364,"role":"BASIC"},"creationTime":1635875774000,"updateTime":1640219714000,"removalTime":null},{"id":"paYvPp","type":"javascript","name":"js copy text to system clipboard","content":"// Hint: this solution works in older browsers.\n// Warning: document.execCommand() function is marked as depricated.\n\n\nwindow.Clipboard = new function() {\n var transit = null;\n var element = null;\n function copy(text) {\n element.value = text;\n element.style.display = 'block';\n element.select();\n document.execCommand('copy');\n element.style.display = 'none';\n }\n window.addEventListener('load', function() {\n element = document.createElement('textarea');\n element.setAttribute('style', 'border: none; width: 0; height: 0; display: none');\n document.body.appendChild(element);\n if (transit) {\n copy(transit);\n }\n });\n this.copy = function(text) {\n if (element) {\n if (element.parentNode == null) {\n throw new Error('Element has been removed from document.');\n }\n copy(text);\n } else {\n transit = text;\n }\n };\n};\n\n\n// Usage example:\n\nClipboard.copy('Hello Dirask!')","source":"https://dirask.com/posts/JavaScript-how-to-copy-text-to-system-clipboard-Kj87Ap","author":{"id":"9oAZR0","name":"Aran-Busby","avatar":"1629131689052__9oAZR0__w40px_h40px.jpg","points":502,"role":"BASIC"},"creationTime":1640219798000,"updateTime":1640220645000,"removalTime":null},{"id":"DgW5rD","type":"javascript","name":"js copy text to system clipboard","content":"// Warning: document.execCommand() function is marked as depricated.\n\n\nvar input = document.querySelector('#input-element');\n\ninput.value = 'Text to copy to clipboard ...'; // replace the text to own one\ninput.select(); // it is required to select inpout text before copy operation\n\ndocument.execCommand('copy'); // copies selected text into the system clipboard","source":"","author":{"id":"7ad4pa","name":"Palpys","avatar":"1629142763139__7ad4pa__w40px_h40px.jpg","points":764,"role":"BASIC"},"creationTime":1640219997000,"updateTime":1644532990000,"removalTime":null}]}}]};