I have list of input area in the form with id like "contact1_title", "contact2_title", "contact3_title" ....
And I also have a list of selection in this form with id like "contact1_name", "contact2_name", "contact2_name" ....
I want to write a loop to dynamic bind the onchange function for the selections.
Here is my code:
for(var j=1;j<6;j++){
$('#contact'+j+'_name').change(function() {
alert(j);
//json.engineer is a json object
$('#contact'+j+'_title').val( json.engineer[this.selectedIndex-1].title );
});
}
When I change the selection, it always alert 6.
and title input area cannot be changed by select.
Does it mean the variable in javascript always change?
How can I bind an action dynamically?
I can hack it by using "this.name.split("_")[0]" but the code seems ugly.
Thanks in advanced.