Skip to content

Instantly share code, notes, and snippets.

@BernhardPosselt
Last active December 11, 2015 21:48
Show Gist options
  • Select an option

  • Save BernhardPosselt/4665085 to your computer and use it in GitHub Desktop.

Select an option

Save BernhardPosselt/4665085 to your computer and use it in GitHub Desktop.

Revisions

  1. BernhardPosselt revised this gist Jan 29, 2013. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,18 @@
    for(var i=0; i<10; i++){
    $elem = $("<p>");
    $elem.click(function(){
    alert(i);
    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);
    }
  2. BernhardPosselt created this gist Jan 29, 2013.
    8 changes: 8 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    for(var i=0; i<10; i++){
    $elem = $("<p>");
    $elem.click(function(){
    alert(i);
    });
    $(document).append($elem);
    }