Reputation: 121
I am trying to add tax if the checkbox is click or not add it, if it is not also if the checkbox is checked after the amount is added to add the tax and vice versa to remove tax if it is unchecked. My code is below but i am getting the error message of updatetotal
is not a function. from what i have read this should work but isn't. please ignore the $j
this is setup for non conflict jquery on the database
$j(function() {
$j(document).on('keyup', '#addit_fee_cost_net', function() {
updateTotal();
});
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
$j(document).on('change','#addit_taxable_flag', function() {
if($j(this).is(":checked")) {
updateTotal();
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total_inc_vat = $j('#addit_fee_cost_gross');
var tax = parseFloat($j('#tax_id').val());
var total_inc_vat = addit_fees * (tax + 1);
if(isNaN(total_inc_vat)){
total_inc_vat=0.00;
}
output_total_inc_vat.val(total_inc_vat.toFixed(2));
};
} else {
updateTotal();
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
}
});
});
I have just added the below answers so i am now not getting the undifined error but the if checked isnt working so nothing alters on change.
$j(function() {
$j(document).on('keyup', '#addit_fee_cost_net', function() {
updateTotal();
});
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
$j(document).on('change','#addit_taxable_flag', function() {
if($j(this).is(":checked")) {
var updateTotal = function () {
updateTotal();
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total_inc_vat = $j('#addit_fee_cost_gross');
var tax = parseFloat($j('#tax_id').val());
var total_inc_vat = addit_fees * (tax + 1);
if(isNaN(total_inc_vat)){
total_inc_vat=0.00;
}
output_total_inc_vat.val(total_inc_vat.toFixed(2));
};
} else {
var updateTotal = function () {
updateTotal();
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
}
});
});
Any help would be greatly appreciated as no matter how i try to format this i cannot get it to run i have just tried the below format but nothing fires. Many thanks in advance
function updateTotal1(checked){
if(checked == true){
var updateTotal1 = function () {
updateTotal1();
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total_inc_vat = $j('#addit_fee_cost_gross');
var tax = parseFloat($j('#tax_id').val());
var total_inc_vat = addit_fees * (tax + 1);
if(isNaN(total_inc_vat)){
total_inc_vat=0.00;
}
output_total_inc_vat.val(total_inc_vat.toFixed(2));
};
} else {
var updateTotal1 = function () {
updateTotal1();
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
};
};
});
$j('#addit_taxable_flag').on('change', function(){
updateTotal($j(this).is(':checked'));
});
Upvotes: 0
Views: 141
Reputation: 121
After a large struggle and some outside help i have managed to resolve the issue. Many thanks for all of your help. apologies here is the code.
$j(function() {
$j(document).on('keyup', '#addit_fee_cost_net', function() {
updateTotal();
});
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var tax = parseFloat($j('#tax_id').val());
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
if($j('#addit_taxable_flag').is(":checked")) {
var total = addit_fees * (tax + 1);
}
output_total.val(total.toFixed(2));
};
$j(document).on('change','#addit_taxable_flag', function() {
if($j(this).is(":checked")) {
updateTotal2();
} else {
updateTotal3();
}
});
var updateTotal2 = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total_inc_vat = $j('#addit_fee_cost_gross');
var tax = parseFloat($j('#tax_id').val());
var total_inc_vat = addit_fees * (tax + 1);
if(isNaN(total_inc_vat)){
total_inc_vat=0.00;
}
output_total_inc_vat.val(total_inc_vat.toFixed(2));
};
var updateTotal3 = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
var output_total = $j('#addit_fee_cost_gross');
var total = addit_fees;
if(isNaN(total)){
total=0.00;
}
output_total.val(total.toFixed(2));
};
});
Upvotes: 1
Reputation:
Before $(document).ready({...}) define your function because you need to define your functions before the "function caller or trigger loads".
function updateTotal(status){
if(status == true){
// if checked
}
else {
// if not checked
}
}
jQuery:
$j('#addit_taxable_flag').on('change', function(){
updateTotal($j(this).is(':checked'));
});
Upvotes: 0
Reputation: 5948
You can't call the function when it stored in a variable before defining the variable! updateTotal is undefined
error should popup in your console.
$j(document).on('keyup', '#addit_fee_cost_net', function() {
updateTotal();//undefined
});
var updateTotal = function () {
addit_fees = parseFloat($j('#addit_fee_cost_net').val());
First define your variable with the function in it and then call it.
var x = function () {};
x();
Upvotes: 0
Reputation: 642
updateTotal() was called before being defined as function. you should put it after the var updateTotal = function(){ ... } . Or, I would simply just directly perform the logic you have .
Upvotes: 0