troggy69 troggy69 - 2 months ago 9
jQuery Question

If checkbox is checked do this else do this Not Working

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'));
});

Answer

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));
    };  

});