/* fix: â??closeâ?? the value of i inside createFunction, so it won't change */ var createFunction = function(i) { return function() { alert(i); }; }; for (var i=0; i<5; i++) { $('

click me

').appendTo('body').click(createFunction(i)); }