7
Imagine that I have a table with a number of rows that varies dynamically (that is, the user can insert more rows) and each row has two columns with two text fields: quantidade
and valor
.
What would be the most performative way to calculate the value of each of these lines and add them together to show the total value of my table?
Currently I am using the following code:
$(document).on('keyup', '#calculaTotal tbody td input', function () {
var total = 0;
$('#calculaTotal tbody tr').each(function () {
var td = $(this).children('td');
total += td.eq(0).children('input').val() * td.eq(1).children('input').val();
});
$('.total').html(total);
});
That can be seen in this example: FIDDLE
The fact is that this table may have n
lines and I believe there are faster ways to do this total calculation. Anyone has any idea how?
Some remarks:
- No final value treatment is required, this will be dealt with later.
- Any solution, both in vanilla Javascript how much with jQuery is welcome.