The alert(i)
onclick bind line is run on 3 divs, but all of them when clicked alert the last set value of i
. I hope what I\'m trying to do makes s
While you could use invoke a function inside the loop, creating a new variable scope which captures the current value of i
, an alternate approach would be to simply take the i
value from the element's ID attribute:
for (var i = 0; i < self.bars[BarsIndex].markers.length; i++) {
$("#bmi-" + self.containerId + "-" + i).bind('click', function() {
//grab the number from the ID of the element
alert( /\d+$/.exec( this.id )[0] );
});
}
Example: http://jsfiddle.net/UGQA7/