I'm trying to understand when bound functions are evaluated. My code below shows "You see 5" on every link while I want it to show the number associated with the link. 5 is of course the value of i after the loop terminates. How can I change my click function so it shows what I want?? Thanks for the help!
<html>
<head>
<title>JQ function evaluation primer</title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
for (var i = 0; i < 5; i++) {
var a = $("<a />").attr({ "href": "#" }).text("Click for " + i);
a.click(function() { alert("You see " + i); });
$("#div1").append($("<div/>")).append(a);
}
});
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>
I may have posted this twice but I don't see the first one. Sorry if moderation is taking longer than I expect.