I’m trying to do some conditional manipulation of elements that are dynamically added to some container on a page, but I’m missing an event.
Say I have a container:
<div id="container"></div>
I can easily bind an event handler to the click function for all new elements, using
$('#container').on('click', '.sub-element', function() { ... });
But what do I bind to in order to get a “one-time” hook when the element is added to #container. I have tried to bind to ready and load to no avail. Is there any way to do this, or do I have to come up with another solution to my problem?
You can trigger a custom event on the newly added DOM element that can be picked-up by a jQuery event handler:
Here is a demo: http://jsfiddle.net/ggHh7/4/
This method allows you to do something globally even if you load the dynamic content through different methods.
Update
I’m not sure of the browser support (I’ll assume IE8 and older don’t support this) but you can use the
DOMNodeInsertedmutation event to detect when a DOM element is added:Here is a demo: http://jsfiddle.net/ggHh7/7/
UPDATE
There is a new API for this as
DOMNodeInsertedis depreciated at this time. I haven’t looked into it but it’s calledMutationOvserver: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver