Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

JavaScript Copy to Clipboard

25 Responses »

Providing your users the ability to copy text to the clipboard can make your website much more convenient and valuable. Unfortunately Flash 10 broke most of the methods for doing so. Luckily a great library named ZeroClipboard exists. ZeroClipboard uses a pinch of Flash and touch of JavaScript to make copying to the clipboard happen.

The Sample HTML

<script type="text/javascript" src="ZeroClipboard.js"></script>
<textarea name="box-content" id="box-content" rows="5" cols="70">
	The David Walsh Blog is the best blog around!  MooTools FTW!
</textarea>
<br /><br />
<p><input type="button" id="copy" name="copy" value="Copy to Clipboard" /></p>

The above HTML features a form element with the ID "box-content" and a button with the ID "copy". Both of those element IDs will come into play with ZeroClipboard.

The ZeroClipboard JavaScript

//set path
ZeroClipboard.setMoviePath('http://davidwalsh.name/dw-content/ZeroClipboard.swf');
//create client
var clip = new ZeroClipboard.Client();
//event
clip.addEventListener('mousedown',function() {
	clip.setText(document.getElementById('box-content').value);
});
clip.addEventListener('complete',function(client,text) {
	alert('copied: ' + text);
});
//glue it to the button
clip.glue('copy');

With the ZeroClipboard object loaded we:

  • Set the path to the SWF file.
  • Create what ZeroClipboard calls a "client": A client is a single instance of the clipboard library on the page, linked to a particular button or other DOM element.
  • Add a mousedown event listener that places text from inside our textarea into the clipboard.
  • Add an optional complete event where we can add functionality to execute once the text has been successfully added to the clipboard.
  • "Glue" the button to the functionality we've added in the steps above.

That's it! ZeroClipboard is an outstanding utility to allow copy to the clipboard functionality on any website.

Discussion

  1. October 19, 2009 @ 9:51 am

    nice code, thanks!!

  2. October 19, 2009 @ 9:52 am

    I have used ZeroClipboard in a project and it does what it has to with no problems.

    P.S. The ZeroClipboard link goes to the demo rather than http://code.google.com/p/zeroclipboard/ .

    Thanks for reminding me this handy resource.

  3. October 19, 2009 @ 10:02 am

    Updated. Thanks Umut!

  4. October 19, 2009 @ 10:04 am

    Useful and clean, … but we still need to use flash anyway … Hopefully we’ll find a flash free way ot do that…

  5. October 19, 2009 @ 10:18 am

    @Opi: A few browsers have their own method of allowing Copy to Clipboard. I didn’t mention those because this solution works in all browsers.

  6. October 19, 2009 @ 10:29 am

    Nice hint, thx!
    I only need the “ZeroClipboard.js” and “ZeroClipboard.swf” from the tar.gz from the ZeroClipboard Homepage right?

  7. October 19, 2009 @ 10:40 am

    @derschreckliche: Yep, just those two files.

  8. ben
    October 19, 2009 @ 6:13 pm

    I used this a few weeks ago on a little internal web app. It seems ok, but I have had the odd random bug – it has attached the client to the wrong DOM element, or the client has disappeared completely.

    Is this what bit.ly uses?

  9. October 20, 2009 @ 4:14 pm

    Great tutorial! Very easy to follow and really nice stuff, keep up the good work.

  10. October 24, 2009 @ 6:53 am

    Сool trick Devid ))

  11. amit
    October 26, 2009 @ 11:00 am

    Hey ya Buddy
    How can i use this Funtion in my Blog Posts :(

    is any way ? plz tell :)

  12. October 30, 2009 @ 7:07 am

    I’ve tried to code but didn’t worked for me… something its wrong on it;
    Please check!

  13. eddie
    November 11, 2009 @ 5:07 am

    Hey David, how would I go about following through with a direct link? I want users on my website to be able to copy the text and then followed by a link to a website.. where it loads it when you click on the copy.

  14. loo
    November 23, 2009 @ 10:31 am

    Hello !
    I’ve tried to make it work but no way ! No errors but nothing happens.
    Do you know if there’s a conflict with jQuery 1.3.2 ?

    Thanks in advance :)

  15. jammer
    December 20, 2009 @ 9:14 pm

    Hi,

    How to give the anchor (link), so that we can open another site and copy the information same time.

    I found this at retailmenot.com site….

    can u please do this

  16. quang nguyen
    January 11, 2010 @ 11:31 pm

    Hi David,
    Thank you very much for really nice tutorial. I made it works myself.
    However still wondering if Zeroclipboard can work with more than one text field and button on one page. Do you have any experience on this?

    Thanks again.
    Quang

  17. February 18, 2010 @ 8:27 pm

    Thank, David) Very useful and clean thing.

  18. March 17, 2010 @ 2:38 am

    hi,

    This script not work on IE7 browser. Please check

  19. shiyam
    March 17, 2010 @ 2:48 am

    @shiyam:

    The script is very useful. It works perfectly on all browser except the IE7

    This script not work on IE7 browse. It displayed the operation aborted error message.

    Please check .

  20. April 2, 2010 @ 3:50 am

    Hi David.. thanks for sharing this. Just a quick not saying that the script is not working for Firefox 3.6.2.

    Thanks

  21. davekingsnorth
    April 15, 2010 @ 6:02 pm

    Ye unfortunately it’s not working in latest version of Chrome or Safari 3.2.2 on Windows XP.

    I thought this would be a 2 minute google job but it looks like there is no straight forward solution

  22. mohammad juned
    May 1, 2010 @ 1:27 pm

    Hi David,
    Thanks for the good code, but is it working on Firefox, Google chrome, Opera etc.
    I see, it only works on IE.

    Thanks

  23. dhruv
    May 20, 2010 @ 1:30 am

    Yeah, your demo page only works with IE. On FF & Chrome, it’s not working.

    Although demo page at

    http://bowser.macminicolo.net/~jhuckaby/zeroclipboard/

    is working fine. I don’t know what’s wrong with your demo page but now I believe that trick should work.

  24. andrew
    June 4, 2010 @ 3:02 am

    It works on FF also, but the div containing the embedded flash is a couple pixels “over” the button. Try clicking between the button and the textbox.

    Thanks for posting this! Neat piece of code.

  25. July 28, 2010 @ 3:53 am

    @David Walsh: Yup, all the other methods are not that great. There are a few which only work in IE geez :| Anyway, great share.. thanks a ton David. You and Chris always save me sometime or the other :P

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!