Get notified when a DOM-Element is added

Possible duplicate:
How to detect when a new element has been added to the document in jquery?

I have a list of comments in a normal ul element. On page load, I use something like (system based on templates)

<li id="someId">Some Content</li>
<script>doStuffWithLi('someId');</script>

      

So, if the page is loaded and there are some li, each based on a template, there is this block for each li.

People can now post a new comment and the template is loaded onto the page again, but this time the script is not executed. I think the js that is loaded after the finished document is not executed at all, the ones that loaded the document?

Unfortunately there is no jQuery function like $('li').live('ready', function() {});

. I tried it with livequery plugin like $('li').livequery(function() {//DO STUFF});

- But that didn't work either. I cannot just add a call to the element creation, because I cannot influence the function of adding a comment.

Is it possible to call the function when adding another li?

+3


source to share


1 answer


You can use event DOMSubtreeModified

. Assign this event to the parent lis and check if the event is fired if there is a new li. In this case, you assign a new handler to this li.



$("#li_parent").bind("DOMSubtreeModified", function() {
    alert("possibly a li has been inserted");
    // do your magic here
});

      

+2


source







All Articles