I have a checkbox that calls a javascript function. When this checkbox is checked, other values in a form are auto filled out. The type of these other values are another checkbox and an option select field. I have the following in html:
<div id="NA" >
<input type="checkbox" name="remove_cd" value="r_on" id="r_on_cd" />N/A:
<select name="reason" id="reason_list">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<font color="#cc0000">Reason Required</font><hr/></div>
In javascript, I have the following in a function:
...
var y = document.getElementById("NA").children;
for(var i=0; i<y.length; y++){
y[i].checked=true;
y[i].options.selectedIndex=2;
}
...
I’m a little confused as to why this isn’t working. When I click on a checkbox in the form, the checkbox under the <div id="NA"> gets checked, but the option in the dropdown doesn’t get changed. Ideas?
You have
y++instead ofi++in your loop. Try thiscalling
.selectedIndexonoptionsthrowsCannot set property 'selectedIndex' of undefined