javascript - How to sum up all the fields with specific selector name start -
i have many span fields, contain numbers in text format. these fields have same first part of name singleprice-23
, singleprice-24
, singleprice-25
etc. how can sum these fields using jquery ? , how same inputs same start of name?
you could:
- find span id starts
singleprice
- iterate on every span , sum value
see following:
var spans = $('[id^=singleprice]'); var sum = 0; spans.each(function(index, value){ sum += +($(value).html()); }); console.log(sum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="singleprice-23">100</span> <span id="singleprice-24">200</span> <span id="singleprice-25">400</span>
Comments
Post a Comment