How do I copy the text inside a div to the clipboard? I have a div and need to add a link which will add the text to the clipboard. Is there a solution for this?
<p class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
<a class="copy-text">copy Text</a>
After I click copy text, then I press Ctrl + V, it must be pasted.
Simplicity is the ultimate sophistication.
If you don't want the text-to-be-coppied to be visible:
jQuery:
HTML:
There is another non-Flash way (apart from the Clipboard API mentioned in jfriend00's answer). You need to select the text and then execute the command
copy
to copy to the clipboard whatever text is currently selected on the page.For example, this function will copy the content of the passed element into the clipboard (updated with suggestion in the comments from PointZeroTwo):
This is how it works:
document.execCommand("copy")
.You can see a quick demo here:
The main issue is that not all browsers support this feature at the moment, but you can use it on the main ones from:
Update 1: This can be achieved also with a pure JavaScript solution (no jQuery):
Notice that we pass the id without the # now.
As madzohan reported in the comments below, there is some strange issue with the 64-bit version of Google Chrome in some cases (running the file locally). This issue seems to be fixed with the non-jQuery solution above.
Madzohan tried in Safari and the solution worked but using
document.execCommand('SelectAll')
instead of using.select()
(as specified in the chat and in the comments below).As PointZeroTwo points out in the comments, the code could be improved so it would return a success/failure result. You can see a demo on this jsFiddle.
UPDATE: COPY KEEPING THE TEXT FORMAT
As a user pointed out in the Spanish version of StackOverflow, the solutions listed above work perfectly if you want to copy the content of an element literally, but they don't work that great if you want to paste the copied text with format (as it is copied into a
input type="text"
, the format is "lost").A solution for that would be to copy into a content editable
div
and then copy it using theexecCommand
in a similar way. Here there is an example - click on the copy button and then paste into the content editable box below:And in jQuery, it would be like this:
The text to copy is in text input,like:
<input type="text" id="copyText" name="copyText">
and, on button click above text should get copied to clipboard,so button is like:<button type="submit" id="copy_button" data-clipboard-target='copyText'>Copy</button>
Your script should be like:For CDN files
note:
ZeroClipboard.swf
andZeroClipboard.js
" file should be in the same folder as your file using this functionality is, OR you have to include like we include<script src=""></script>
on our pages.Even better approach without flash or any other requirements is clipboard.js. All you need to do is add
data-clipboard-target="#toCopyElement"
on any button, initialize itnew Clipboard('.btn');
and it will copy the content of DOM with idtoCopyElement
to clipboard. This is a snippet that copy the text provided in your question via a link.One limitation though is that it does not work on safari, but it works on all other browser including mobile browsers as it does not use flash
With Line Breaks (Extention of the Answer from Alvaro Montoro)