Given that I have 5 input fields that are linked to their own price, I want to be able to grab the value of the input multiplied by its specific price and return that value to the dom.
This is what's being rendered, there are 5 with a unique price. I'm not sure how to link each input to its specific price, add the prices of all 5 inputs (if there are any) and post them on the dom.
I'm trying to get the data-price
of the .class
p tag, loop through them, link it to their input
add the sum and post the sum to the dom
<section data-itemid="${menuObj.id}">
<div class="amount">
<input type="number" placeholder="Quantity" min="0" name="${menuObj.id}">
</div>
<div class="item">
<h1>${menuObj.name}</h1>
<p class="food">${menuObj.description}</p>
<p class="price" data-price="${menuObj.price}">${menuObj.price}</p>
</div>
</section>
I have it working for one, but not sure how to get them working for all
$(function() {
$('input').on('keyup', function() {
var input = $('.amount input').val();
var price = $('.price').data('price');
var final = $('.finalprice p');
var total = input * price;
final.text(total);
});
})
For dynamic content use event delegation to bind events.
Use
input
event to cover every change on your inputQuantity
.Apply a
class
to the section. i.e:.menu-class
Approach
Basically, loop over the section and collect the amount.
Snippet