How to copy text to the client's clipboard using jQuery?

mager picture mager · Oct 8, 2009 · Viewed 274.5k times · Source

The workflow is simple:

  1. You click inside a textarea.
  2. The text is copied to the client's clipboard.
  3. Display notice to the user.

How do you do it?

Answer

halocursed picture halocursed · Oct 8, 2009

Copying to the clipboard is a tricky task to do in Javascript in terms of browser compatibility. The best way to do it is using a small flash. It will work on every browser. You can check it in this article.

Here's how to do it for Internet Explorer:

function copy (str)
{
    //for IE ONLY!
    window.clipboardData.setData('Text',str);
}