...

View Full Version : How to take a value from a Select Box



glenelkins
07-11-2006, 03:03 PM
ok so a select box (<select name="blabla"><option>bla</option</select) how do i trap the option selected (onClick event) and place the value selected into a text box? I am a PHP programmer and the page refresh issue gets in the way of what i need to accomplish, javascript can do it!

Cheers

Brandoe85
07-11-2006, 03:16 PM
You can use the onchange event handler, try this:


<html>
<head>
<script type="text/javascript">
function move(sel, txt)
{
txt.value = sel.options[sel.selectedIndex].value;
}
</script>
</head>
<body>
<form>
<select name="test" onchange="move(this, txt);">
<option value="boo1">boo1</option>
<option value="boo2">boo2</option>
<option value="boo3">boo3</option>
<option value="boo4">boo4</option>
</select>
<br>
<input type="text" name="txt">
</form>
</body>
</html>


Good luck;

glenelkins
07-11-2006, 07:57 PM
Thanks

One question though, the function "move()" in the onchange="move(this, txt)" what is "this"?? its not defined anywhere

Brandoe85
07-11-2006, 08:36 PM
'this' refers to the select element. It would be the same as passing it's name to the function.
Heres a good reference:
http://www.quirksmode.org/js/this.html

Good luck;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum