asked on November 20, 2015
•
Show version history
Hi, I am looking to add two totals together from two tables and place the value outside of the table into another field. After using the code below, I keep getting the result NaN and not sure where to look.
.totala + .total = .totalc
$(document).ready(function () {
var totalc;
$('.cf-table-block').on('blur', 'input', sumtotal);
function sumtotal() {
$('.total input').attr('readonly', false);
var hours = 0;
var rate = 0;
var ODC = 0;
var LC = 0;
var AO = 0;
var SGA = 0;
var Profit = 0;
var total = 0;
var subtotal = 0;
$('.cf-table-block tbody tr').each(function () {
hour = parseNumber($(this).find('.hours input').val());
rate = parseNumber($(this).find('.rate input').val());
ODC = parseNumber($(this).find('.ODC input').val());
LC = parseNumber($(this).find('.LC input').val());
AO = parseNumber($(this).find('.AO input').val());
SGA = parseNumber($(this).find('.SGA input').val());
Profit = parseNumber($(this).find('.Profit input').val());
subtotal = (hour * rate)+ODC+LC+AO+SGA+Profit;
$(this).find('.total input').val(subtotal.toFixed(2));
total += subtotal;
totalc += total;
});
$('.totalc input').val(totalc);
}
$('.cf-table-block').on('blur', 'input', sumtotala);
function sumtotala() {
$('.totala input').attr('readonly', false);
var quantity = 0;
var unitcost = 0;
var freight = 0;
var tax = 0;
var totala = 0;
var subtotala = 0;
$('.cf-table-block tbody tr').each(function () {
quantity = parseNumber($(this).find('.quantity input').val());
unitcost = parseNumber($(this).find('.unitcost input').val());
freight = parseNumber($(this).find('.freight input').val());
tax = parseNumber($(this).find('.tax input').val());
subtotala = (quantity * unitcost)+freight+tax;
$(this).find('.totala input').val(subtotala.toFixed(2));
totala += subtotala;
totalc += totala;
});
$('.totalc input').val(totalc);
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
0
0