1

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.

3

3 Answers 3

1

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(){
     ...
})
Sign up to request clarification or add additional context in comments.

Comments

0

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.