0
I’m having trouble recalculating the value of the OTHERS field with the TOTAL value. I need to add the value placed in the other field the first time, if I change the precise value recalculate and add the difference: For example the total value is 29.00 if I put other value 10.00 is 39.00 but if I change to 20.00 calculate the difference and get the result 49.00. Currently my problem is that it is always increasing when I change the value of the other field and adding in total, if I put 10,00 and change the focus, calculates correct, but if I change to 20,00 adds 10,00 + 20,00.
Following is illustration and code image:
let total = 0;
$('#valout').on('change', function(){
let valout = $(this).val();
let subtot = replaceNumber($('#totalpedido').val());
if(isNaN(valout)) valout = 0;
if(isNaN(subtot)) subtot = 0;
if(valout > 0) {
$('#valout').data('val-default', valout);
total = subtot + valout;
} else {
let def = $('#valout').data('val-default');
total = subtot - def;
}
$('#totalpedido').val(replaceDecimal(total));
});
In my opinion you should run the calculation in a separate function, isolate the calculation behavior and after certain action you perform this function, so both in the output of Focus and in the change would always have the same behavior of executing the same function, who would always be doing the calculation from the start.
– Felippe Tadeu
Try to explain this problem better, divide it into items, I was very confused when reading.
– Eduardo Ribeiro
Check that this reply help you. As @Felippetadeu commented, you’d better separate the function from the calculation and call as the events you need.
– 8biT
I edited here I put a gif, notice the total value
– wribeiro
But your gif is so small and blurry that you can’t even see anything.
– Sam