jquery - Sum multiple inputbox values -
i have bunch of inputboxes class "selectme quantity" , sum amount of together.
for example:
<input class="selectme quantity" value="4"> <input class="selectme quantity" value="1"> <input class="selectme quantity" value="5">
how can run through these inputs , display total value (it 10)?
i'd suggest:
function summate (selector) { var sum = 0; $(selector).each(function(){ sum += parsefloat(this.value) || 0; }); return sum; } summate('.selectme.quantity');
or, if you're working browser supports array.reduce()
:
var sum = $('.selectme.quantity').map(function(){ return this.value && parsefloat(this.value); }).get().reduce(function(a,b){ return parsefloat(a) + parsefloat(b); }); console.log(sum);
references:
Comments
Post a Comment