0

I have an object that has an option to create a function on certain event, and I want to create many instances of this object with a loop:

var dragging;
var dragObj = new Array();
for(i=0; i<20; i++){
   dragObj[i] = new Draggy('btn_id_'+i, {
        restrictY:false, 
        restrictX:false,
        onChange: function(){
            dragging = i;
            console.log(dragObj[i].position);
        }
    }
}

Basically I select elements with id 'btn_id_'+i that obtains the draggy object, this allows elements to be dragged and onChange (drag) I can execute a function, in this case I just want to trace in the console the position of the element being dragged and save into a variable the index of the element that is being dragged.

This doesn't work since "onChange" will be executed in a certain moment when the loop is done and i is equal to tha last index of my loop (20).

How can I execute the loop allowing the function inside "onChange" to maintain the value of i at the moment when the loop created the function.

Thanks.

multimediaxp
  • 9,348
  • 13
  • 49
  • 80

0 Answers0