shinjuo shinjuo - 6 months ago 21
HTML Question

Select dropdown menu option with javascript

I have a dropdown menu and I cannot figure out how to make a javascript function select a drop down menu option. I have tested the output of the variables and they are all correct, but it still will not select the option when clicked. Here is the function and drop down menu.

Function

function formFill(a, b, c){
theform.from.value = a;
theform.to.value = b;
for(var i = 0;i < document.getElementById("stateSelect").length;i++){
if(document.getElementById("stateSelect").options[i].value == c ){
document.getElementById("stateSelect").selected = true;
}
}
}


Menu item

<select id="stateSelect" name="stateSelect">
<option value="none">(None)</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>

Answer

Change the line that reads:

document.getElementById("stateSelect").selected = true;

to:

document.getElementById("stateSelect").selectedIndex = i;