I have written the following script. The first part multiplies the quantity and cost per item in a row, and adds it as the value in the ".price" input.
The second part I am attempting to dynamically update the #total value as each .price is added. But I can't get this part to work.
var total;
$('.invoice').on('focus','.current' , function(){
// Calculate Price
$('.qty').keyup(function(){
var qty = $( document.activeElement ).val();
var value = $( document.activeElement ).siblings(".cost").val();
$( document.activeElement ).siblings(".price").val(qty * value);
});
// Calculate Total
$('.price').change(function(){
$('.price').each(function(){
price = $('.price').val();
total = parseInt(total) || 0 + parseInt(price) || 0;
$('#total').val(total);
});
});
});
totalbefore theeachloop and usetotal = total + price;and then assign the total after the each loop. But why are you.keyup()and.change()functions inside theon()function?