0

I write some code in Javascript where I have created a set of elements named "Card" (Card0, Card1, ...). I want to set a loop where I activate in each of these elements the same function with the same parameters, like this:

for (var i = 0; i < n; i++) {
  name = '#Card' + i;
  x1 = i + 1;
  domain.activateElement($(name), function() {
    dosomething(x1)
  });
}

The problem is that the parameters are passed as references so when function dosomething is called in every element it uses value x1=n instead of the value it was initially assigned in each case.

How can I avoid this?

From other posts I have figured out I should create multiple closures giving different names to the attached functions like (not working):

domain.activateElement($(name), function() func+i {
    dosomething(x1)
  });

Thank you for your responses.

0 Answers0