Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: get values of all children of a div

Tags:

jquery

I am trying to use jquery to get the values of all children element of a div.

<div class='parent'>
    <input type='text' value='1' class='child' />
    <input type='text' value='2' class='child' />
    <input type='text' value='3' class='child' />
    <input type='text' value='4' class='child' />
</div>

The child inputs are generated with php from db. Their number varies.

I am trying to get something like:

variable = (child1_value + child2_value + child3_value + child4_value + ... + "childX_value");

how can I make this work for whatever number of children?

like image 574
user1885099 Avatar asked Dec 03 '25 11:12

user1885099


1 Answers

You can use each...

var total = 0;

$(".child").each(function() {
    total += parseInt($(this).val());
});

Or similar

like image 61
Jeff Watkins Avatar answered Dec 06 '25 05:12

Jeff Watkins



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!