Possible Duplicate:
How do JavaScript closures work?
I am trying to use the value of a loop counter inside another function inside the loop. I know this is related to function closures. Its getting a little confusing. I figure if someone can explain on this problem, I'd understand.
for (var i = 0; i < foo.length; i++) {
// $('<li/>').addClass('box').append($('<img/>').attr({'src':foo[i].image, 'data-phrase':foo[i].phrase}).hide()).appendTo($('ul.boxPack'))
$('<li/>').addClass('box')
.attr('data-phrase', foo[i].phrase)
.bind('click', function (i) {
alert(i);
console.log(foo[2]);
$(this).append($('<img/>').attr('src', foo[2].image))
})
.appendTo($('ul.boxPack'));
}
I am unable to alert i
.