Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Trigger Event on .children().size() change

Tags:

html

jquery

css

I have searched near and far but I can't figure out how to put a listener on the size() of children elements. What I am trying to do is:

I have a div with anywhere from 0-6+ child elements. This number isn't defined because I am making a shopping cart. Instead of onclick for every button that can change the number of items in the cart (i.e. add to cart btn, empty cart button, change quantity, etc...) I want to have a listener that when $("#cart").children().size() changes, an if statement checks if the #of different items in the cart is larger than 3. If so change the height of the div.

an example of the shopping cart layout:

<div id='cart'>
     <div class='item'></div>
     <div class='item'></div>
</div>

The actual code site is at: http://www.jamesendres.com/projects/ad/throw-pillows.html

like image 754
meepz Avatar asked Oct 21 '25 17:10

meepz


2 Answers

I'm affraid div doesn't have the change event, so it would be impossible to listen it. On the other hand, you can create your own handler to get over this situation.

I've create a fiddle showing the proof of concept: link.

The code is very simple and I think you can understand it easily. Hope this helps.

like image 139
sigurd Avatar answered Oct 23 '25 06:10

sigurd


You probably want "DOMNodeInserted" event.like:

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

See this answer: https://stackoverflow.com/a/5033719/907753

like image 42
vinit Avatar answered Oct 23 '25 06:10

vinit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!