Is there an event in jQuery which fires on dom element creation? I tried load, but that won't work for example with a span.
-
2Share you code? what you tried?Ahsan Rathod– Ahsan Rathod2012-07-10 09:13:35 +00:00Commented Jul 10, 2012 at 9:13
-
What are you trying to achieve?Ulhas Tuscano– Ulhas Tuscano2012-07-10 09:14:34 +00:00Commented Jul 10, 2012 at 9:14
-
possible duplicate of DOM callback for node creation and maybe How to catch creation of DOM elements and manipulate them with jQuery.Felix Kling– Felix Kling2012-07-10 09:15:51 +00:00Commented Jul 10, 2012 at 9:15
3 Answers
you can try DOMNodeInserted event:
A user agent must dispatch this event type when a node other than an Attr node has been added as a child of another node. A user agent may dispatch this event when an Attr node has been added to an Element node. This event must be dispatched after the insertion has taken place. The event target of this event must be the node being inserted.
$(document).bind('DOMNodeInserted', function(){
...
})
Comments
few references that can help you are, http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-mutationevents https://github.com/jollytoad/jquery.mutation-events
but What you are trying to achieve is what currently all frameworks are doing likes of ember, angularjs and many more. i recommend you to learn one of them especially angular i think what you want to achieve its direct answer is there.
Comments
You can take the function of the creation of the element out into a closure and trigger another function as one new element is created such as
var newElement=function(newElement){
var k=$(newElement);//Create a new element
/*You can add extra operations to the element*/
triggerFunction(someVariable); //Function triggered after the creation of new element
};
var triggerFunction=function(someVariable){//Some function};
Hope the code below explains the situation well.Here we have an observer which will be triggered whenever a new element is created.