Thanks Karl, I really appreciate the help!
On Jul 24, 7:17 pm, "Karl Rudd" <[EMAIL PROTECTED]> wrote: > The "i" in the click function (closure) is just a reference to the "i" > outside. That "i" changes with each iteration of the for loop. > > If you want to save a copy of the value of "i" then create a _local_ > var _inside_ the closure and assign it the value of "i". > > For example: > > for (var i = 1; i < totalPages + 1; i++) { > $("#pages_panel").append($("<span> " + i + "</span>") > .click(function() { var saved = i; pageClicked(saved); > console.log("i:" + saved); })); > > } > > Closures are tricky constructions till you get used to them. > > Karl Rudd > > On Fri, Jul 25, 2008 at 12:00 PM, Kalvin <[EMAIL PROTECTED]> wrote: > > > 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!