https://jsfiddle.net/12xmodp9/1/
When doing each with jquery, how do I use the number selected each time?
eg. in this case I would expect the alert to display the number of the element clicked on.
<div id="foo"></div>
<script type="text/javascript">
for (i = 0; i < 20; i++) {
$('#foo').append(
$('<a>',
{href: '#',
id: "foo_" + i,
html: i
}).on("click", function(){alert(i)})
);
}
</script>
Instead it always displays "20"