Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on load of dynamic element

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?

This fiddle contains my non-working example.

like image 679
Klaus Byskov Pedersen Avatar asked Mar 07 '12 22:03

Klaus Byskov Pedersen


2 Answers

You can trigger a custom event on the newly added DOM element that can be picked-up by a jQuery event handler:

//bind to our custom event for the `.sub-element` elements
$('#container').on('custom-update', '.sub-element', function(){
    $(this).html('<b>yaay!</b>');
});

//append a new element to the container,
//then select it, based on the knowledge that it is the last child of the container element,
//and then trigger our custom event on the element
$('#container').append('<div class="sub-element">No worky!</div>').children().last().trigger('custom-update');

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 DOMNodeInserted mutation event to detect when a DOM element is added:

$('#container').on('DOMNodeInserted', '.sub-element', function(){
    $(this).html('<b>yaay!</b>');
})

$('#container').append('<div class="sub-element">No worky!</div>');

Here is a demo: http://jsfiddle.net/ggHh7/7/ ​

UPDATE

There is a new API for this as DOMNodeInserted is depreciated at this time. I haven't looked into it but it's called MutationOvserver: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

like image 189
Jasper Avatar answered Oct 01 '22 09:10

Jasper


After reading the above answer I now use this successfully:

$('body').on({
   DOMNodeInserted : function(){
      alert('I have been added dynamically');
   },
   click : function(){
      alert('I was clicked');
   },
   '.dynamicElements'
});

Now all my dynamicElements can do stuff as they're loaded, utilizing the awesome .on() event map.

like image 25
tim Avatar answered Oct 01 '22 09:10

tim