Good morning, I have the following script which adds the real-time values of 5 input, they all have the class input_values_provisionals and shows them in a span of id total_provisionals
The question is that if for example the sum goes to 20 at the time of typing any of them and human error in one of them was not a 5 if not a 3, to delete the 5 and put the sum should be 17 and not 20 since I would be really subtracting a 2
Well, it turns out that it continues adding without brake, it shows me 23
What I can think of is that I should first validate what value there is currently, I did it with:
valor_actual=$(this).val();
to then compare it with the new value entered and so add or subtract the difference, I tried to do it but it takes the written value because as the function is with the event "change" because it will actually take me the new value entered and not the one that was already, here the code without the line that I tried
$(document).ready(function() {
//guardara el total
var total = 0;
//cuando haya un cambio en el input
$('.input_valores_provisionales').change(function() {
//valido que sea un numero
if (!isNaN($(this).val()))
{
//va acumulando el total
total += Number($(this).val());
//muestra el total en el span
$("#total_provisionales").text("$"+total);
}
else
{
//Si no introduce numero genera alerta
alert("Debe ingresar un valor numerico");
}
});
});