Summary: I have passed the name of the id select tag into an Array. I want to dynamically assign each Select Item to display the Index of the selected item upon change. This is sent to the output div. Only the third select item is dynamically being triggered. Why?
<script language="JavaScript" type="text/javascript">
$(document).ready(function(){
var NameOfSelect=new Array("a","b","c");
for (i=0;i<NameOfSelect.length;i++){
var sel=NameOfSelect[i];
$("#"+NameOfSelect[i]).change(function () {
var str = "";
$("#"+sel+" option:selected").each(function () {
str += $(this).index() + " "+$("#"+sel).attr("id");
});
$("#output").text(str);
})
.trigger('change');
}
</script>
The HTML
<select id="a" >
<option value="0a" >0a</option>
<option value="1a" >1a</option>
<option value="2a" >2a</option>
</select>
<select id="b" >
<option value="0b" >0b</option>
<option value="1b" >1b</option>
<option value="2b" >2b</option>
</select>
<select id="c" >
<option value="0c" >0c</option>
<option value="1c" >1c</option>
<option value="2c" >2c</option>
</select>
<div id=output></div>
The output shows that only "C" is being triggered on change. Why are "a" and "b" not being similarly dynamically assigned?