Imagine that I have a table with a number of rows that varies dynamically (that is, the user can insert more rows), each row having 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 sum them to show the total value of my table?
Currently I'm 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);
});
What can be seen in this example: FIDDLE
The fact is that this table can have n
lines and I believe there are faster ways to do this total calculation. Anyone have any idea how?
Some comments:
- No end value processing is required, this will be dealt with later.
- Any solution in both vanilla JavaScript and jQuery is welcome.