I am using .on()
to attach click events to multiple elements that appear on the page dynamically. The problem I have is that when I add .on
to a container on the page and want to attach click events to multiple elements in the container, the latter overwrites the previous.
<div id="container">
<!-- elements here appear dynamically -->
<div id="id1"></div>
<div id="id1"></div>
</div>
<script>
$('#container').on("click", "#id1", function(){});
$('#container').on("click", "#id2", function(){});
</script>
In the above example only the click event for id2 works.
Is there a way around this?
Thank you, Ev.