Is there a jQuery event to determine when the list of children has changed?

I hope to find a jQuery event that will tell me when the collection of items has grown or shrunk after adding / removing a child.

+4
source share
2 answers

You can count the children first, then use the conditional operator to check if the quantity has changed.

<div> <span></span> <span></span> <span></span> <span></span> <span></span> </div> alert($('div').children().length); 
+1
source

What you are looking for are the DOMNodeInserted and DOMNodeRemoved events.

For example, after binding to the list event of the DOMNodeInserted list:

 $('ol').bind('DOMNodeInserted', function() { alert('node inserted'); }); 

and then another list item is added:

 $('ol').append('<li>x</li>'); 

the event is fired.

+9
source

Source: https://habr.com/ru/post/1340219/


All Articles