I click a radio button which updates a hidden field with a value for a Tax Rate based on the province selection.
// Set Tax Rate
$(document).ready(function() {
$('.province input').on('click', function() {
$('.taxrate input').val($(this).val()).trigger('change');
});
});
I then use the following code ( I realize this may not be the most efficient method but with limited JQuery experience I essentially modified one of your code examples to get this to work) to calculate the Tax based on the chosen rate.
// Calculate the Tax
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.taxrate').on('blur', 'input', sumtotal);
$('.subtotal').on('blur', 'input', sumtotal);
$('.province').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('.subtotal input').val(s);
// sum += s;
sum += parseNumber($('.taxrate input').val()) * parseNumber($('.subtotal input').val());
});
$('.tax input').val((sum).toFixed(2));
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
I added this line above the the Tax Rate will calculate when the Province radio button is selected "province' is my css value.
$('.province').on('blur', 'input', sumtotal);
This works great and updates when the radio button loses focus. Is it possible to have the Tax Rate update on click.
I tried changing the line to:
$('.province').on('click', 'blur', 'input', sumtotal);
or
$('.province').on('focus', 'blur', 'input', sumtotal);
but these Methods do not seem valid being used in the above line of code.