Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

option select in javascript`

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
hey guys,

i have the following code that outputs a drop down. however in javascript how can i set the the value of the drop when a user selects a button.

<SELECT NAME="partnumber">
<OPTION VALUE="7382">steam turbine
<OPTION VALUE="2928">resistor array
<OPTION VALUE="3993">widget analyzer
<OPTION VALUE="9398">fiber identifier
</SELECT>

thanks guys
 
Code:
document.forms["myForm"].elements["dropDown"].value = someValue;


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
hey tsdragon thanks for that but i was wondering if you could give me an example, im new to Javascript.

oh if you know of any good javascript tutorial site let me know

thanks
 
[1] The question is not very clear to start with.
[2] The "value" of the select-tag is not rightaway intuitive.
[2.1] It is read-only (ff/nn) or
[2.2] It's got ignored if being assigned a value attribute on select-tag (ff/nn/ie); and
[2.3] if write something to it dynamically, it either ignores it (ff/nn) or disturbes the select index to being -1 (nothing selected, ie).
- tsuji
 
This should do it.
===========================================
<html>
<head>
<script>
function setListValue(val){
document.partNumberForm.partnumber.value=val;
}
</script>
</head>
<body>
<form name="partNumberForm">
<table>
<tr>
<td align="center">
<SELECT NAME="partnumber">
<OPTION VALUE="-1">select part number
<OPTION VALUE="7382">steam turbine
<OPTION VALUE="2928">resistor array
<OPTION VALUE="3993">widget analyzer
<OPTION VALUE="9398">fiber identifier
</SELECT>
<br /><br />
<button onClick="setListValue('7382');">steam turbine</button>&nbsp;
<button onClick="setListValue('2928');">resistor array</button>&nbsp;
<button onClick="setListValue('3993');">widget analyzer</button>&nbsp;
<button onClick="setListValue('9398');">fiber identifier</button>
</td>
</tr>
</table>
</form>
</body>
</html>
===========================================
- shu
 
Glad shu745 bring this up again.

Upon re-reading my post above, I was not summing up the issue very clearly and in exact terms.

You sure can "write" the available option value to a select element. But what I was trying to express is that you cannot write whatever to it. Furthermore, if you write whatever freely to it, browsers behaves differently. Msie will show you nothing selected (selectedIndex=-1), whereas mozilla will just ignore what you've "written" to it.

If what written is intended to change a particular option value individually, you have to delete it and insert a new Options to it and not just write something to it.

Another morbid case is if we casually do anything non-sensical like <select name="..." value="shouldnotbehere">, that "value" attribute will be ignored as out-of-place.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top