Here's a fiddle I made:
https://jsfiddle.net/sucwcokv/4/
It adds the total, but I would echo what @Marcel Wasilewski said, it is not a good idea to run this kind of price calculation only in javascript, as it'd be quite open to user manipulation.
One problem with the fiddle; it seems to have a problem with the accuracy of the number, it adds recurring 9's sometimes, for instance. I've seen this in other languages when variables are converted between types, but I haven't yet figured it out for this. Could someone confirm what causes this? Any help would be appreciated.
I used JQuery to speed things up, but if you want pure JS... you may need to look into converting it:
$(document).ready(function() {
var subtotals = [];
$('.input-text').change(function() {
updateTotal();
});
});
function getRowSubtotal(rowToTotal) {
var currentQty = $(rowToTotal).find('input').val();
var thisPrice = $(rowToTotal).find('ins').find('span.amount').text();
thisPrice = thisPrice.replace('€', '');
thisPrice = thisPrice.replace(',', '.');
return Number(thisPrice) * currentQty;
}
function updateTotal() {
subtotals = [];
$('.input-text').each(function() {
var thisRow = $(this).parent().parent().parent();
subtotals.push(getRowSubtotal(thisRow));
});
var total = 0;
for (var i = 0; i < subtotals.length; i++) {
total += subtotals[i];
}
$('#total').text('Total: ' + total + '€');
console.log(total);
}