-1

When I loop this array with a for loop it somehow gives me undefined in

updateField(this.value, vehicles[i]+"_brand"

While

$("#"+vehicles[i]+"_year").change(function(){

Does get the right values of the array. How can this be and how can I solve this?

var vehicles = new Array();
    vehicles[0] = "auto";
    vehicles[1] = "truck";
    vehicles[2] = "motor";

for(var i = 0;i < vehicles.length;i++){
    $("#"+vehicles[i]+"_year").change(function(){
       updateField(this.value, vehicles[i]+"_brand", 1, 2, this.parentNode.id), resetBelow(0,'auto'), show('auto_brand');
    });
}
Sinan Samet
  • 6,432
  • 12
  • 50
  • 93

2 Answers2

1

The time the change event will be fired vehicles[i] will be evaluated again and you will be having the last value of i.

Change

vehicles[i]+"_brand"

To

this.id.split('_')[0]+"_brand"
Adil
  • 146,340
  • 25
  • 209
  • 204
0

It looks like you're having commas where semicolons should be to me:

updateField(this.value, vehicles[i]+"_brand", 1, 2, this.parentNode.id); resetBelow(0,'auto'); show('auto_brand');

... or maybe your updateField() function has more parameters and you simply closed the function prematurely?

Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50