2

I need a simple script that gives me a button that when clicked will copy the contents of a text input box to the clipboard. Or it could be a script that when you click on the text input box, automatically copies the contents to the clipboard and displays a "Copy" message next to the text input box.

I've seen them all over the web, but can't find the code for one now.

techguy
  • 31
  • 3
  • What are you trying to copy *to*—another form field, a JavaScript variable, or the clipboard? – Dori Jul 19 '10 at 22:58
  • Maybe I should have clarified that I'm looking for it to copy the contents of the text input box to the clipboard. – techguy Jul 19 '10 at 22:58
  • possible duplicate of [How to copy to the clipboard in JavaScript?](http://stackoverflow.com/questions/400212/how-to-copy-to-the-clipboard-in-javascript) – Tim Cooper Jul 26 '12 at 13:11
  • See: http://stackoverflow.com/questions/400212/how-to-copy-to-clipboard-in-javascript – CJM Jul 19 '10 at 23:11

1 Answers1

1

This example uses jQuery:

Assuming an input box with an id of foo, and a button with an id of clickme, here's how I'd do it:

var inputText = "";
$("#clickme").click(function() {
    inputText = $("#foo").val();
});
// inputText now has the input box's value

Edit:

After your clarification, I now understand what you are trying to do. Unfortunately, flash 10 broke most of the methods to do this. However, some great people wrote ZeroClipboard, which is fully compatible with flash 10 and makes it really easy to accomplish this task. Their wiki explains usage.

Alex
  • 58,815
  • 45
  • 146
  • 176