Copy text field text to clipboard when button is clicked

I want to create a jQuery (or javascript) button that selects everything in textarea and then copy the text to your clipboard when the button is clicked.

I found some examples using a focal event. But I'm looking for a button that you really have to click to select and copy.

How can i do this?

+20
javascript jquery copy textarea
source share
3 answers

You need to use select() to select the textarea text and use execCommand('copy') to copy the selected text. His work is in the top version of browsers.

 $("button").click(function(){ $("textarea").select(); document.execCommand('copy'); }); 

You can also do this work without jquery as shown below

 document.querySelector("button").onclick = function(){ document.querySelector("textarea").select(); document.execCommand('copy'); }; 
 <button>Select</button> <br/> <textarea></textarea> 
+45
source share

This can be done without using jQuery.

Here is a pure JS solution.

 function copy() { let textarea = document.getElementById("textarea"); textarea.select(); document.execCommand("copy"); } 
 <textarea id="textarea"></textarea> <br> <button onclick="copy()">Copy</button> 
+6
source share

## HELP ##


copy

This one does not work. Do you have a code that copies everything inside the text area without opening a new page? What I want to do is click the "Copy" button, and everything inside the text area will be copied to the clipboard. Please help.

0
source share

All Articles