Something really strange is happening to my checkboxes checked in my table.
I have built an Invoice calculator in a Table, you can see it here and my purpose is that once i check the inputs ( Iva or Irpef) automatically they are not calculated in the Total Amount.
As you can see in the example i have linked, if you check the Irpef Input checkbox, it works well, the Irpef is not calculated in the Total Amount. The problem is that it does not happen the same with my Input Checkbox IVA, why? The code is exactly the same.
When i try just to make them working separately, it’s fine, both work well, so it looks like that they can not work together, it might be? What’happening? I need your help on this issue.
thanks
Here the code related to the issue:
function tally(selector) {
var total = 0;
$('p.editable_number').each(function() {
total += parseInt($(this).text()) || 0;
$('#subtotal').html(total);
if($("#iva").is(':checked')){
$('#subtotal').html((total).toFixed(2));
$('#total').html((total*0.00).toFixed(2));
$('#total1').html((total*0.15).toFixed(2));
$('#total2').html((total*0.85).toFixed(2));
}
else {
$('#subtotal').html((total).toFixed(2));
$('#total').html((total*0.21).toFixed(2));
$('#total1').html((total*0.15).toFixed(2));
$('#total2').html((total*1.06).toFixed(2));
}
if($("#irpef").is(':checked')){
$('#subtotal').html((total).toFixed(2));
$('#total').html((total*0.21).toFixed(2));
$('#total1').html((total*0.00).toFixed(2));
$('#total2').html((total*1.21).toFixed(2));
}
else {
$('#subtotal').html((total).toFixed(2))
$('#total').html((total*0.21).toFixed(2));
$('#total1').html((total*0.15).toFixed(2));
$('#total2').html((total*1.06).toFixed(2));
}
})
}
$('#irpef').change(function() {
tally('p#subtotal');
tally('p#total');
});
$('#iva').change(function() {
tally('p#subtotal');
tally('p#total');
});
Try this one, should be fine now. I think you were trying to do an else if and do everything a couple of times instead of once (each function). refreshed
I added variables to keep track of the total/subtotal and vat/irpf.
each()goes through all the p.editable_number elements, so you were executing the same code a couple of times, every time overwriting the previous results, you get the right thing because it’s the end loop that counted. Now you are just getting the values once and then calculating the relevant subtotal, vat, irpf and total values(based on the condition of vat or irpf being checked or not).