Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector with sum

I didn't know a good title for my problem, this is why I didn't search.

My problem is the following:

I have a HTML code, for example:

<input type="text" id="product_qty_1" value="12" />
<input type="text" id="product_qty_2" value="21" />
<input type="text" id="product_qty_3" value="45" />

I'd like to make a sum of the values by jQuery, but this need to be automatic, because we can't know how many inputs are there.

I don't know, what to do with the selector input[id*="product_qty_"] to get it working, and make a sum of the values!

Thanks in advance!

like image 836
Skylineman Avatar asked Dec 21 '22 18:12

Skylineman


1 Answers

var total=0;

$("input[type='text']").each(function() {
    total += parseInt(this.value, 10);
});
like image 83
adeneo Avatar answered Dec 23 '22 09:12

adeneo