views:

186

answers:

3
<select name="products">
    <option value=""> - Choose - </option>
    <option value="01">table</option>
    <option value="02">chair</option>
    <option value="03">book</option>
    <option value="04">car</option>
</select>

I want to be able to copy text automatically on selecting a particular option...
Eg. If I select book, book should automatically be copied, so I can paste(Ctrl+v) it somewhere else..Thanks

A: 

If by "copy" you mean extract the value, you should give each option an ID attribute. Then, access it by using code like this:

bookText = document.getElementById("book").innerHTML;
hansengel
no I mean copy, for copy/paste... like I can paste it somewhere else
halocursed
This doesn't happen onchange event.
rogeriopvl
+3  A: 

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.

Still, just for giving you a hint, here's how to do it for Internet Explorer (just because it is the easiest one to do):

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

Now your select calls the javascript function onchange event. This event is triggered whenever a new value is selected:

<select name="products" onchange="copy(this.options[this.selectedIndex].innerHTML)">
    <option value=""> - Choose - </option>
    <option value="01">table</option>
    <option value="02">chair</option>
    <option value="03">book</option>
    <option value="04">car</option>
</select>
rogeriopvl
Well this is for my personal(admin control) use only and will not be shown to the user...
halocursed
rogerio it is not wworking!!!
halocursed
@halocursed, just corrected the code. try again please
rogeriopvl
Thank you!!rogerio... It's working Now!
halocursed
One more thing...Thanks a lot for the link
halocursed
A: 

In Internet Explorer, you can copy text to the clipboard using code similar to the following:

<script type="text/javascript">
 function sendToClipboard(s)
 {
   if( window.clipboardData && clipboardData.setData )
   {
         clipboardData.setData("Text", s);
   }
   else
   {
     alert("Internet Explorer required");
   }
 }

 window.onload = function()
 {
   document.getElementById("products").onchange = function()
   {
     sendToClipboard(this.options[this.selectedIndex].innerHTML);
   };
 };
</script>

FireFox makes it sufficiently more difficult (not a bad thing) to enable clipboard access. See http://kb.mozillazine.org/Granting%5FJavaScript%5Faccess%5Fto%5Fthe%5Fclipboard and https://addons.mozilla.org/en-US/firefox/addon/852?application=firefox&amp;id=852, which is an addon that allows you to configure sites for clipboard functionality.

I'm not aware of what code to use to make the work, however.

David Andres
It's not working at my end...:(
halocursed
It's not working at my end(checked in ie7)...:(
halocursed
@halocursed: updated the window.onload function to set the onchange property of the select element instead of change. Try it now.
David Andres