I am trying to build a simple table that if the user enters any miles. It multiplies that value by 0.535 and puts it into the Total field.
Otherwise the user can just input a value into the Total field if they don't need to input any miles.
Likewise I need to sum all the Total values into a Grand Total field outside of the Table.
Here is the table and Javascript I am trying to work with and the classes so you can see which fields I am trying to call in the Javascript.
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
//Run if Miles field NOT Empty
if(($(this).find('.miles input').val() != ""){
var s = 0;
var miles = 0;
var fueltotal = 0;
miles = parseNumber($(this).find('.miles input').val())
fueltotal = (miles * 0.535).toFixed(2);
s = $(this).find('.total input').val((fueltotal).toFixed(2));
sum += s;
}
//Run if Miles Empty
else{
var s = 0;
s = $(this).find('.total input').val();
sum += s;
}
});
$('.grandtotal input').val((sum).toFixed(2));
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? alert('Not Float') : f; //treat invalid input as 0;
}
});