I want to calculate each row's total price by multiplying individual row's inputs and then finally calculate a grand total by adding all the total values of the Total column using JQuery. I am only getting the first row to display total when I enter values. Any help will be greatly appreciated.
blade
@foreach($service->invoices as $invoice)
<tr>
<td class="text-right">{{ $invoice->description }}</td>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="custom{{ $invoice->id }}" id="custom{{ $invoice->id }}">
<label class="custom-control-label" for="custom{{ $invoice->id }}"></label>
</div>
</td>
<td>
<div class="form-row justify-content-center">
<div class="form-group mb-0">
<div class="input-group mx-auto mb-0">
<div class="number-input amount">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" id="decrease"></button>
<input class="quantity bg-light" id="quantity" min="0" placeholder="0" name="quantity" value="0" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus" id="increment"></button>
</div>
</div>
</div>
</div>
</td>
<td class="price">{{ $invoice->price }}</td>
<td class="total">0</td>
</tr>
@endforeach
script
<script>
$("#decrease , #increment , .quantity").on("click input", function() {
var selectors = $(this).closest('tr'); //get closest tr
var quantity = selectors.find('.quantity').val(); //get qty
if (!quantity || quantity < 0)
return;
var price = parseFloat(selectors.find('.price').text());
var total = (price * quantity);
selectors.find('.total').text(total); //add total
mult += total;
$("#grandTotal").text(mult);
})
</script>