[jQuery] Why does my closure not seem to share scope?
var a = $("<span><<</span>").click(function()
{ pageClicked(page-1); });
$("#pages_panel").empty().append("Page: ").append(a);
for (var i = 1; i < totalPages + 1; i++) {
$("#pages_panel").append($("<span> " + i + "</span>")
.click(function() { pageClicked(i); console.log("i:" + i); }));
}
It properly prints out 1 2 3 4 5 etc., but when clicking on any of
them, the console.log output of this code is "i:6" for every page. So
I guess it's referring to the value of i after the for loop is
completed.
How can I get it to reflect only the value of i during the time of
creation?...
Thanks!