By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Automatically change values in a drop down select box in a form

P: 89
Hello All

I need some help in a form i'm creating. I have to fields with drop down select boxes. What i want to do is, when a user selects a given value in one drop down, the other drop down should atumatically change to another value.

Ex: the user choses the number "4" in one box, then the other box should change to "4 WD".

Here is my code:

[HTML]<select name="job___num_layers" onChange="document.getElementById('parray_div').st yle.display=(this.selectedIndex>1)?'block':'none'; ">
<option value="2" selected="selected">2</option>
<option value="4">4</option>
<option value="6">6</option>
<option value="10">10</option>
<option value="14">14</option>
<option value="18">18</option>
</select>

<select name="job___num_layers">
<option value="3WD" selected="selected">3 WD (Standard)</option>
<option value="4WD">4 WD (standard)</option>
<option value="5WD">5 WD (standard)</option>
<option value="7WD">7 WD (standard)</option>
<option value="8WD">8 WD (standard)</option>
<option value="9WD">9 WD (standard)</option>
</select>[/HTML]
Feb 19 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Do the values which have to be set have the same selectedIndex as the first drop down? If they do, just use the selectedIndex, e.g. secondSelect.selectedIndex = firstSelect.selectedIndex. Put this in a function and call it onchange of the first drop down. I assume you know how to access elements within the page.
Feb 19 '08 #2

P: 89
Do the values which have to be set have the same selectedIndex as the first drop down? If they do, just use the selectedIndex, e.g. secondSelect.selectedIndex = firstSelect.selectedIndex. Put this in a function and call it onchange of the first drop down. I assume you know how to access elements within the page.
Could you post an example of what you mean...??? They do not have the same selectedIndex. The selectedIndex that is used on the first drop down is unique for that drop down, and has nothing to do with the second drop down.
Feb 19 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Could you post an example of what you mean...??? They do not have the same selectedIndex. The selectedIndex that is used on the first drop down is unique for that drop down, and has nothing to do with the second drop down.
I meant them matching up, e.g. the option with value 4 has a selectedIndex of 1 which is also the selectedIndex of "4WD" in the second drop down. Anyway, if they don't match up, use the value instead and use an array like the following:
Expand|Select|Wrap|Line Numbers
  1. var arr = {"4":"4WD","6":"5WD","10":"7WD"};
  2. document.getElementById(secondSelectID).value = arr[val]; //val is value of 1st drop down
to match them.
Feb 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.