Question

Id there an event that can get triggered when a jQuery dynamically created element is added to any part of the DOM. This would be useful to perform with and height calculation on elements only when any portion of the code in a script is attaching the element to the page or any other page children.

Was it helpful?

Solution

Sorry about my previous answer, I didn't pay enough attention to what you were asking.

What you're really after is DOM Mutation Events, which has been deprecated, and would be replaced in the future by DOM Level 4 Mutation Observers, which looks promising but isn't implemented in current browsers. Here's a great post about the history and future of this topic.

Meanwhile, if you don't have any actual access to what's inserting the new nodes (e.g. a 3rd party plugin which doesn't fire the needed events), your best bet would sadly be polling a container for its children().length.

OTHER TIPS

There is a jQuery plugin called livequery that provides the functionality you require.

For example :

$('.something').livequery(function() { 
    $(this).trigger('event');
}); 

the above code will execute when a DOM element with the class of something is added to the DOM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top