Skip to content

Instantly share code, notes, and snippets.

@BernhardPosselt
Last active December 11, 2015 21:48
Show Gist options
  • Save BernhardPosselt/4665085 to your computer and use it in GitHub Desktop.
Save BernhardPosselt/4665085 to your computer and use it in GitHub Desktop.
for(var i=0; i<10; i++){
$elem = $("<p>");
$elem.click(function(){
alert(i); // alerts 9 every time the element is being clicked
});
$(document).append($elem);
}
// solution
for(var i=0; i<10; i++){
$elem = $("<p>");
$elem.click(function(){
function(value){
alert(value);
}(i);
});
$(document).append($elem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment