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