Jquery How to sum up input values in real time? [closed]

-1

I'm a beginner and I have a system where the user can add several inputs dynamically, so when typing the values (in decimals) the total sum is displayed in a div. If anyone can help me, I'll be grateful.

    
asked by anonymous 18.11.2018 / 03:10

1 answer

1

You can use the jQuery keyup () function to perform an action whenever any number is entered into an input.

Keyup documentation: link .

$(".NomeClasseInputs").keyup(function(){
    var soma = 0;
    $(this).each(function() {
        soma += parseFloat($(this).val());
    });
})

The keyup () will be executed whenever there is some action inside some input, and the function inside is going through each input and doing the sum of the values.

Remembering that for this to work all inputs must have the same class. The sum will be stored in the sum variable, which you can insert into any div with .html ():

$('.idDiv').html(soma);
    
18.11.2018 / 03:27