I've created a dynamic table where users have to enter the student name and their subject and marks. Each student can contain several subjects. I have attached the code below, which would be easier to understand.
Click here to view the working code
What is working :
The sum is working perfectly fine for 1 block of students that is added.
What I need
The sum should add for each block of students that I dynamically add and dispaly the totals in sub-total. The sub-totals should add to give the total.
Code
function calculateSum() {
var sum = 0;
$(".marks").each(function () {
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$("#Sub").val(sum.toFixed(2));
$("#Sub1").val(sum.toFixed(2));
}
function recalc() {
var tt = 0;
$("#tb").find('tr').each(function () {
$(this).find('input.row-total').val(dateTotal ? dateTotal : "");
tt += isNumber(dateTotal) ? dateTotal : 0;
}); //END .each
$("#table-grand-total").html(tt);
}
function calculateTotal() {
var sum = 0;
//iterate through each textboxes and add the values
$(".row-total").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length !== 0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$(".table-total").val(sum.toFixed(2));
}