I have a list of You need to bind the event handler to a common ancestor of the elements on which it should be triggered. For example, if your This would be for an HTML structure like so: The reason this works is that DOM events bubble up the tree from the point at which they originate. The Also note that it's invalid to have duplicate Finally, if you are using jQuery 1.7+ you should use the on method instead. It will have the same effect, but notice the reversal of the first 2 arguments: You are adding
#element
gets appended inside a div
with an id
of parent
:$("#parent").delegate(".commenticon", "click", function() {
//Do stuff
});
<div id="parent">
<div class="element">
</div>
<div class="element">
</div>
</div>
delegate
method captures the event at an ancestor element and checks whether or not it originated at an element matching the selector.id
values in the same document. Here I've changed your element
ID values to class names instead.$("#parent").on("click", ".commenticon", function() {
//Do stuff
};
<div id="element> ... </div>
which means that those doesn't exist initially. You need to add delegate to upper level that exists. Then any .commenticon
that is added under the "container" will have click event.$('#container').delegate(".commenticon","click" , function() {
$(this).closest('.actionframe').next('nav').slideToggle(300);
return false;
});