In my test code, I have a simple div that I'm using as a container for 5 loop created div elements. I try adding a click function to all 5 div elements, but only the last one is given a click function.
<div id="testbed"></div>
<script type="text/javascript">
$(document).ready(function () {
for (i = 0; i < 5; i++) {
$("#testbed").html($("#testbed").html() + "<div id='" + i + "'>Hello!</div>");
$("#" + i).click(function () {
alert(i);
});
}
});
</script>
Interestingly enough, instead of alerting 4, it alerts 5. I don't know why it's only applying the click function to the last div element and not the first 4.