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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • printline
    New Member
    • Jul 2006
    • 89

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

    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="docum ent.getElementB yId('parray_div ').style.displa y=(this.selecte dIndex>1)?'bloc k':'none';">
    <option value="2" selected="selec ted">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="selec ted">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]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.se lectedIndex = firstSelect.sel ectedIndex. 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.

    Comment

    • printline
      New Member
      • Jul 2006
      • 89

      #3
      Originally posted by acoder
      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.se lectedIndex = firstSelect.sel ectedIndex. 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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by printline
        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: [code=javascript]var arr = {"4":"4WD","6": "5WD","10":"7WD "};
        document.getEle mentById(second SelectID).value = arr[val]; //val is value of 1st drop down[/code]to match them.

        Comment

        Working...