Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <p> <button id="copyTextBtn">Copy Textarea</button> <textarea id="copytextarea">Selcet and copy text</textarea> </p> <script> copyTextBtn = document.querySelector('#copyTextBtn'); copyTextBtn.addEventListener('click', function(event) { let copyTextarea = document.querySelector('#copytextarea'); copyTextarea.focus(); copyTextarea.select(); try { let successful = document.execCommand('copy'); let msg = successful ? 'successful' : 'unsuccessful'; alert('Copy text command was ' + msg); } catch(err) { alert('Unable to copy'); } }); </script> </body> </html>