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?
You can use each...
var total = 0;
$(".child").each(function() {
total += parseInt($(this).val());
});
Or similar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With