Copy text to form parameter tag using javascript?
<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 automatically copy text when a specific option is selected ...
For example. If I select a book, the book should be copied automatically, so I can paste (Ctrl + v) it somewhere else. thank
source to share
Copying to clipboard is a tricky task to do in Javascript from a browser compatibility perspective. The best way to do this is with a small flash. It will work in every browser. You can check it out in this article .
However, just to give you a hint, here's how to do it for Internet Explorer (simply because it's the easiest way to do it):
function copy (str)
{
//for Internet explorer ONLY!
window.clipboardData.setData('Text',str);
}
Now your choice is calling the javascript onchange function. This event fires on every new value:
<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>
source to share
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 hard enough (not bad) to provide buffer access. See http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard and https://addons.mozilla.org/en-US/firefox/addon/852?application=firefox&id=852 which is an addon that allows you to customize sites for features clipboard.
I don't know what code to use to get the job done.
source to share