views:

167

answers:

3

If you wanted to change the value of the hidden field to something not in the value of the dropdown like so

<form>
    <select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
        <option value="foo">One - 42</option>
        <option value="bar">Two - 40</option>
        <option value="wig">Three - 38</option>
    </select>

    <input type="hidden" name="hiddenInput" id="hiddenInput" value="" />
</form>

And i want to pass (if two selected) dropdown="bar" and hiddenInput="40"

The value has to be passed but needs to effect the hiddenfield.

What do you think? Would you need to If then? or could you have it something like

<form>
    <select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
        <option value="foo" onchange="set hiddenInput - 42">One - 42</option>
        <option value="bar" onchange="set hiddenInput - 40">Two - 40</option>
        <option value="wig" onchange="set hiddenInput - 38">Three - 38</option>
    </select>

    <input type="hidden" name="hiddenInput" id="hiddenInput" value="" />
</form>
A: 

I'd keep the mapping on the server rather then trying to handle it client side.

Forget the hidden input. Just look up the value based on the value of the dropdown after the data reaches the server.

David Dorward
A: 

Unfortunately, you'd need the IF statement. You can use the onclick event for the option tags in Firefox and probably other browsers, but in IE you can't set any events on the option elements, only on the select element.

Andy E
A: 

You have the right idea with the onchange attribute. Your function could look like:

function changeHiddenInput(mySelect) {

    var map = {foo: "42", bar: "40", wig: "38"};
    var index = mySelect.selectedIndex;
    var value = mySelect.options[index].value;

    var hiddenInput = document.getElementById("hiddenInput");
    hiddenInput.value = map[value];
}

The map variable there maps each option's value on to what the hidden attribute should be set to.

Matt Bridges
How would i call this function on the change? <select onchange="changeHiddenInput(this);">
The way you have it written in the original question is correct. When the user changes the value of the dropdown box, the function will be called. Try adding alert('The value of the dropbox was changed'); to the top of the function and you'll get a popup box every time it is called.
Matt Bridges
here is the javascript i am using <script type="text/javascript"> function changeHiddenInput(exam_type_category) { alert('The value of the dropbox was changed'); var map = {SAT: "1", ACT: "4", College Admissions: "40"}; var index = exam_type_category.selectedIndex; var value = exam_type_category.options[index].value; var hiddenInput = document.getElementById("exam_type_id"); hiddenInput.value = map[value]; } </script>
and this is the form <input type="hidden" name="program_type_id" value="4" /> <input type="hidden" id="radius" name="radius" value="20" /> <input type="hidden" id="exam_type_id" name="exam_type_id" value="1" /> <table> <tr> <td> <select id="exam_type_category" name="exam_type_category" onchange="changeHiddenInput(this);"> <option value="SAT" selected="selected">SAT</option> <option value="ACT">ACT</option> <option value="College Admissions">College Admissions</option> </select>
</td> <td> <input id="zip_code" name="zip_code" size="7" type="text" value="Zipcode" onFocus="if(this.value == 'Zipcode') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Zipcode';}"> </td> <td> <span><input name="commit" value="Search" type="submit"></span> </td> </tr> </table> </form>