Pick up values with jQuery

Asked

Viewed 181 times

1

I have a following field,

 <input name="dados[<?= $i ?>][quantidade]" class="quantidade" type="text" value="<?= isset($dados['quantidade']) ? $dados['quantidade'] : '' ?>" id="quantidade"/>

And I have a modal that when I choose a new food, it adds another field like the one above, I have to take these values with jQuery and make a sum, how and what I do to catch all these fields?

1 answer

3

You can get by using the $.each:

var soma = 0;

$('.quantidade').each(function(index) {
    soma += parseInt($(this).val());
});

alert('Soma: ' + soma);

I see you’re using the same ID quantidade for all elements, this is wrong, each element must have a unique identification.

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.