The problem is that as you iterate through the loop, i
is incremented. It ends up with a value of 6. When you say alert(i)
you are asking javascript to tell you what the value of i
is at the time the link is clicked, which by that point is 6.
If you want to get the contents of the box instead you could do something like this:
for (var i = 1; i < 6; i++) {
console.log(i);
$("#div" + i).click(function(e) {
alert($(this).text());
});
}
Working example: http://jsfiddle.net/rmXcF/2/