Hello, I am fairly new at javascript and am trying to complete, what I think, is a fairly simple task.... I am pounding my head trying to figure it out.
I have a form that uses a table with quantity and price then total for the line. This part works and below is the code I am using. The subtotal of all table lines populates a field outside the table called "Subtotal". I am selecting the province at the top of the form for the address and that province field fills a hidden field called "Tax Percentage" with the decimal version of the tax rate. (ie. 0.13). I am looking to have the javascript calculate the appropriate tax amount using the "Subtotal" field (CSS: subtotal) multiply it with the populated "Tax Percentage" field (CSS: taxpercent) then put the calculated tax amount in a field called "Tax" (CSS: tax) and the total amount in the "Total" field (CSS: total).
List of CSS classes:
Field: Subtotal CSS: subtotal
Field: Tax Percentage CSS: taxpercent
Field: Tax CSS: tax
Field: Total CSS: total
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
$(this).find('.cost input').val(s);
sum += s;
$('.subtotal input').val(sum);
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});