I have a table with the following structure and data
<tr>
<td><input id="c1" type="checkbox"/></td>
<td>342</td>
<td>24</td>
<td>Jeff</td>
</tr>
<tr>
<td><input id="c2" type="checkbox"/></td>
<td>343</td>
<td>29</td>
<td>Libo</td>
</tr>
I want to calculate total value of the third column of all rows, which are checked. So assuming both rows above are checked, the value of third column will be 24+29=53
var total = 0;
$("table tr:has(:checkbox:checked) td:nth-child(3)").each(function() {
total += parseInt($(this).text());
});
See this fiddle.
var total = 0;
Array.prototype.slice.call(document.getElementById('tableID').rows).
forEach(function(row) {
if (!row.childNodes[1].firstChild.checked) return;
total += parseFloat(row.childNodes[2].innerHTML);
});
The OP asked to count only those that are checked. This answer only counts checked rows.
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