Reputation: 54252
I have a table of sub-totals.
<table>
<tr>
<td><span id="subtotal_1">100</span></td>
</tr>
<tr>
<td><span id="subtotal_2">150</span></td>
</tr>
</table>
Here is my Javascript code to calculate grand total :
var grandtotal = 0;
$('span[id^=subtotal_]').each(function() {
grandtotal += parseInt($(this).val());
});
but the grandtotal
returns nothing. What did I miss?
Upvotes: 0
Views: 199
Reputation: 2010
just change the .val() to .text()
var grandtotal = 0;
$('span[id^=subtotal_]').each(function() {
grandtotal += parseInt($(this).text());
});
Upvotes: 1
Reputation: 388406
You need to use .text() here, .val() is for input fields
var grandtotal = 0;
$('span[id^=subtotal_]').each(function() {
grandtotal += parseInt($.trim($(this).text()));
});
Demo: Fiddle
Upvotes: 1