View Full Version : Set values of multiple fields with one drop down selection

05-25-2005, 04:47 PM
Basically what I need to do is set value of 2 hidden fields based on the selection of the drop down.

My situation is that I need to set a Cost Center Code and specify an Authorizing Manager based on the region the user selects from a drop down.

I'd prefer not to use a bunch of 'if' statements, but I don't know how else I can do this.

Does anyone have any ideas?

Eternity Angel
05-25-2005, 05:14 PM
Use an array within an array to control which value was selected.

<script type="text/javascript">
ops = new Array();
ops[1] = new Array(45,32);
ops[2] = new Array(12.5,409);
ops[3] = new Array("some text","some other text.");

function setvals(a) {
if (typeof(ops[a]) == "undefined") return false;
v1 = document.getElementById('hidden1');
v2 = document.getElementById('hidden2');
v1.value = ops[a][0];
v2.value = ops[a][1];

// Delete this alert if you actually use this code:
alert('Hidden field 1 = '+v1.value+'\n\nHidden field 2 = '+v2.value);

<form action="..." method="...">
<input type="hidden" name="hidden1" id="hidden1" value="">
<input type="hidden" name="hidden2" id="hidden2" value="">
Select an option: <select name="the_select" id="the_select" onchange="setvals(this.selectedIndex);">
<option>-Pick one-