I'm attaching some DIVs dynamically in JQuery and want to attach each one a click event, this is how I do it
for (var i=0; i<CAItems.length; i++)
{
//alert(i);
$('#UserWorldItems').append(
$("<div id='ContextItm" + i + "' class='UserItem'><div class='UserItemBox'>" +
CAItems[i].Context + "</div><div class='UserItemBox AddPaddingToItem'>" +
CAItems[i].Action + "</div></div>").on("click" , function() { alert(i); } )
);
}
The DIVs are created but the function that is fired when clicking any of the new divs results in an alert saying "11" (11 is the last value of i). How can I resolve this that each div will show an alert with the right index? (0,1,2,3....11)