Sorry for the headline - I couldn’t understand the way he phrased.
Here's the script:
I have a function that creates an element:
buildSelect(id,cbFunc,...)
Inside buildSelect, it does this:
select.attachEvent('onchange',cbFunc);
I also have an array that goes:
var xs = ['x1','x2','x3'...];
Given all this, I have code that does this:
for(var i = 0; i < xs.length; i++) { buildSelect(blah,function(){ CallBack(xs[i],...) },...); }
The problem is that when onchange starts on one of these samples, it correctly goes to CallBack (), but the first parameter is incorrect. For example, if I change the third choice, I expect CallBack () to be called with xs [2], instead, I get some variable things like xs [3] or something else.
If I change this a bit:
for(var i = 0; i < xs.length; i++) { var xm = xs[i]; buildSelect(blah,function(){ CallBack(xm,...) },...); }
I am still getting the wrong values in CallBack (). Something tells me that this is about the scope / closure, but I can't figure that out.
I just want the first choice to call CallBack to exchange with the first parameter as xs [0], the second select with xs [1] and so on. What can i do wrong here?
I must clarify that xs is a global variable.
thanks