[JavaScript] Copy to Clipboard
Copy content of textarea to clipboard via JavaScript.
Source Code for Demo (html):
1 2 3 4 5 6 7 8 9 10 | <!doctype html> <html> <head><title>JavaScript Copy to Clipboard</title></head> <body> <textarea id="text">Hello World</textarea> <br> <button id="copy">Copy Textarea</button> <script src="copy.js"></script> </body> </html> |
Source Code for Demo (JavaScript):
1 2 3 4 5 6 7 8 9 10 11 12 | var textareaElm = document.getElementById("text"); var copyBtnElm = document.getElementById("copy"); copyBtnElm.onclick = function(event) { textareaElm.select(); var isSuccessful = document.execCommand('copy'); if (isSuccessful) { textareaElm.value = "Copy OK"; } else { textareaElm.value = "Copy Fail"; } } |
This comes from the answer of [1]. See the answer for browser support.
Tested on: Chromium 49.0.2623.108 Ubuntu 15.10 (64-bit).
References:
[1] |