I have multiple input arrays (see snippet). I need to calculate all of the entered values to a variables. It is an invoice creation, so I want to know the total price of all items with discount. How can I achieve this?
This is what I have for now:
$('#addNewPosition').on('click', function(e) {
e.preventDefault();
$('#positions').append('<input type="text" name="item[]" placeholder="Item" class="form-control">\n' +
' <input type="number" step="any" name="price[]" placeholder="Price" class="form-control">\n' +
' <input type="number" name="item_discount[]" placeholder="Discount, %" class="form-control">\n' +
' <input type="number" step="any" name="quantity[]" placeholder="Quantity" class="form-control">\n' +
' <input type="text" name="quantity_index[]" placeholder="Unit" class="form-control">\n' +
' <input type="text" name="description[]" placeholder="Description" class="form-control">\n');
});
// Get the number of all items added:
var count = $("input[name='item[]']").map(function() {
return $(this).val();
}).get().length;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="positions">
<input type="number" step="any" name="price[]" placeholder="Price" class="form-control">
<input type="number" name="item_discount[]" placeholder="Discount, %" class="form-control">
<input type="number" step="any" name="quantity[]" placeholder="Quantity" class="form-control">
<input type="text" name="quantity_index[]" placeholder="Unit" class="form-control">
<input type="text" name="description[]" placeholder="Description" class="form-control">
<a href="#" id="addNewPosition" class="btn btn-green">Add new</a>
</div>
All I want to achieve is to calculate the total price: total = ((each field's price) - discount, %) * quantity
Is there any way I can do that? Kind regards
.lengthThat is giving you the array length you just created. You should use reduce() after the mapforloop