I have a table with inputs in each cell, except for one (.total), which determines the total number of each row in the table. When one of the inputs changes, I would like the sum to change for this row. Below is as far as I could get.
HTML:
<table>
<tr>
<td><input value = "100"/></td>
<td><input value = "100"/></td>
<td><input value = "100"/></td>
<td class="total">300</td>
</tr>
<tr>
<td><input value = "200"/></td>
<td><input value = "200"/></td>
<td><input value = "200"/></td>
<td class="total">600</td>
</tr>
</table>
Now for jQuery:
$(document).ready(function(){
$("input").each(function() {
$(this).keyup(function(){
newSum();
});
});
});
function newSum() {
var sum = 0;
var thisRow = $(this).closest('tr');
$(thisRow).("td:not(.total) input:text").each(function() {
sum += this.value;
});
$(thisRow).(".total").html(sum);
}
Any help in figuring out what I did wrong will be greatly appreciated. Thank!
source
share