Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jquery event that fires when a new node is inserted into the dom?

Tags:

html

jquery

Is there an event jquery fires on a dom element when it is inserted into the dom?

E.g. lets say I load some content via ajax and add it to the DOM and in some other piece of javascript I could add an event using .live() so that when an event matching a specific selector is added to the DOM the event fires on said element. Similar to $(document).ready() but on content that is added to the DOM dynamically.

What I am dreaming of:

$('.myClass').live('ready', function() {     alert('.myClass added to dom'); }); 

If jquery doesn't support such a thing then is there another way I could achieve this functionality without modifying the code that actually does the initial dom manipulation?

like image 216
3urdoch Avatar asked Oct 10 '10 11:10

3urdoch


People also ask

Which method is triggered when an element is added to the DOM?

The actual answer is "use mutation observers" (as outlined in this question: Determining if a HTML element has been added to the DOM dynamically), however support (specifically on IE) is limited (http://caniuse.com/mutationobserver). So the actual ACTUAL answer is "Use mutation observers....

How does jQuery work with DOM?

jQuery provides methods such as attr(), html(), text() and val() which act as getters and setters to manipulate the content from HTML documents. Document Object Model (DOM) - is a W3C (World Wide Web Consortium) standard that allows us to create, change, or remove elements from the HTML or XML documents.

What is event bubbling in jQuery?

Event bubbling directs an event to its intended target, and works like this: When an object (like a button) is clicked, an event is directed to the object. If an event handler is set for the object, the event handler is triggered. Then the event bubbles up (like a bubble in water) to the objects parent.


1 Answers

deprecated: as per @Meglio's comment below, the DOMNodeInserted event is deprecated, and will be removed from the web at some unkown time in the future. For the best results, start learning about the MutationObserver API.

see @dain's answer below.

If you bind the 'DOMNodeInserted' event to the document or body directly, it will get executed every time anything is inserted anywhere. If you want the callback to run only when a particular kind of element is added, you would be wise to use delegated events.

Usually, if you are adding a class of elements to the DOM you will be adding them to a common parent. So attach the event handler like this:

$('body').on('DOMNodeInserted', '#common-parent', function(e) {   if ($(e.target).attr('class') === 'myClass') {     console.log('hit');   } }); 

Basically the same answer as Myke's above, but since you are using a delegated event handler rather than a direct event handler, the code in there will be fired less often.

NOTE:

$('body').on('DOMNodeInserted', '.myClass', function(e) {   console.log(e.target); }); 

This seems to work too... but I don't know why.

like image 158
Ziggy Avatar answered Sep 28 '22 17:09

Ziggy