Scope troubles in Javascript when passing an anonymous function to a named function with a local variable
Sorry about the title - I couldn't figure out a way to phrase it. Here's the scenario: I have a function that builds a 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 of these, I have some code that does this: for(var i = 0; i < xs.length; i++) { buildSelect(blah,function(){ CallBack(xs[i],...) },...); } The issue is that when onchange gets fired on one of those selects it correctly goes to CallBack() but the first parameter is incorrect. For example if I change