views:

43

answers:

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.

+1  A: 

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
This sample seems to be missing the actual sample
Pim Jager
see my answer. :-)
Alex
+1 for jQuery. IMHO nobody should use javascript without it*
Tim
Sorry, I clarified that I need it to copy to the clipboard.
techguy