0

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);
  });
})

2 Answers 2

1

For dynamic content use event delegation to bind events.

$(document).on('input', '.menu-class div[class="amount"] input[type="number"]', function() {...}

Use input event to cover every change on your input Quantity.

Apply a class to the section. i.e: .menu-class

Approach

Basically, loop over the section and collect the amount.

var total = 0;
$('.menu-class').each(function() {
  var $section = $(this);
  var input = $section.find('div.amount [type="number"]').val();
  var price = $section.find('.price').data('price');

  total += (input * price);      
});

$finalPrice.text(total);

Snippet

$(function() {
  var $finalPrice = $('.finalprice p');
  $finalPrice.on('calculate', function() {
    var total = 0;
    $('.menu-class').each(function() {
      var $section = $(this);
      var input = $section.find('div.amount [type="number"]').val();
      var price = $section.find('.price').data('price');

      total += (input * price);
    });

    $finalPrice.text(total);
  });

  $(document).on('input', '.menu-class div[class="amount"] input[type="number"]', function() {
    $finalPrice.trigger('calculate');
  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class='col-md-6 col-xs-6'>
  <section class='menu-class' data-itemid="111">
    <div class="amount">
      <input type="number" placeholder="Quantity" min="0" name="quant_111">
    </div>
    <div class="item">
      <h1>My Meat :-)</h1>
      <p class="food">Meat</p>
      <p class="price" data-price="1000.0">1,000.00</p>
    </div>
  </section>
  <hr>
  <section class='menu-class' data-itemid="112">
    <div class="amount">
      <input type="number" placeholder="Quantity" min="0" name="quant_112">
    </div>
    <div class="item">
      <h1>My bread :-)</h1>
      <p class="food">Bread</p>
      <p class="price" data-price="2000.0">2,000.00</p>
    </div>
  </section>
</section>
<section class='col-md-6 col-xs-6'>
  <section class='finalprice'>
    <h1>Final price</h1>
    <p>
    </p>
  </section>
</section>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. What If I have 5 inputs and I want to track the total of each input combined, not each separately. Do you know a solution for this? It's what I was asking.
@RogerFedFan see the updated answer. Basically, when the quantity input is changed, an event called calculate will be trigged to calculate the values.
Thank you so much!
0

$('.amount input') returns an array-like object of all dom elements that match the selector. I believe $('.amount input').val(); will return value from the first item in the array

You may want to localize finding inputs, like below:

$(function() {
    $('input').on('keyup', function() {
        var $section = $(this).closest('section');
        var input = $section.find('.amount input').val();
        var price = $section.find('.price').data('price');
        var final = $section.find('.finalprice p');
        var total = input * price;
        final.text(total);
    });
});

1 Comment

Thanks for the answer. However, var final is not displaying the change. When i console logged it, it gives me a blank value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.