0

I am trying to find the product of all the sum of all .quantity_for_sum values in a nested .each() loop with non-nested data attribute ('#product_price") as so:

.js

$('.quantity_for_sum').change(function() {

 var price = 0;
 var quantity = 0;

    $('.product_list_item').each(function() {
          $this = $(this);

        price = +$this.children('#product_price').data('price');

        $('.quantity_for_sum').each(function() {
            productPrice += +this.value*price
        });
    }); 
     $('.total_count').html(productPrice);
});    

.html.erb

<div class="row product_list_item">
    <div id="product_price" data-price="80.00" class="product_right">
        $80.00
    </div>
    <div class="product_right"> 
        <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
        <select class="quantity_for_sum" id="order_units_attributes_1_quantity">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
    </div>
</div>
<div class="row product_list_item">
    <div id="product_price" data-price="100.00" class="product_right">
        $100.00
    </div>
    <div class="product_right"> 
        <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
    </div>
</div>

I then want to sum all productPrice values for each of these loops. I am having lots of trouble getting the nesting.

3
  • Are you sure it will be price = +$this.children('#product_price').data('price'); instead of price += $this.children('#product_price').data('price'); Commented Jan 12, 2016 at 4:48
  • just added html...ok i see Commented Jan 12, 2016 at 4:54
  • just updated parsed html Commented Jan 12, 2016 at 5:08

2 Answers 2

1

$('.quantity_for_sum').each is not needed as there is always single select element in each container.

$('.quantity_for_sum').change(function() {
  var productPrice = 0;
  $('.product_list_item').each(function() {
    var $this = $(this);
    var price = +$this.find('.product_right').data('price');
    var val = $this.find('.quantity_for_sum').val();
    productPrice += +val * price
  });
  $('.total_count').html(productPrice);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row product_list_item">
  <div id="product_price" data-price="80.00" class="product_right">
    $80.00
  </div>
  <div class="product_right">
    <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
    </select>
  </div>
</div>
<div class="row product_list_item">
  <div id="product_price" data-price="100.00" class="product_right">
    $100.00
  </div>
  <div class="product_right">
    <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
  </div>
</div>
<div class="total_count"></div>

Fiddle here

If you can have multiple select in .product_list_item container, then while selecting .quantity_for_sum, you must refer the context($this) as it will find the select element from that parent.

$('.quantity_for_sum').change(function() {
  var productPrice = 0;
  $('.product_list_item').each(function() {
    $this = $(this);
    var price = +$this.children('#product_price').data('price');
    $this.find('.quantity_for_sum').each(function() {
      productPrice += +this.value * price
    });
  });
  $('.total_count').html(productPrice);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="row product_list_item">
  <div id="product_price" data-price="80.00" class="product_right">
    $80.00
  </div>
  <div class="product_right">
    <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
    </select>
  </div>
</div>
<div class="row product_list_item">
  <div id="product_price" data-price="100.00" class="product_right">
    $100.00
  </div>
  <div class="product_right">
    <select class="quantity_for_sum" id="order_units_attributes_0_quantity">
      <option value="0">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
  </div>
</div>
<div class="total_count"></div>

Refer this fiddle => https://jsfiddle.net/bsew1a0d/2/

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

1 Comment

Works perfectly! Thank you so much!
0

Js code:

$(document).ready(function() {
  $('.quantity_for_sum').change(function() {

  var price = 0;
  var quantity = 0;
  var productPrice = 0

  $('.product_list_item').each(function() {
    $this = $(this);
    price = +$this.children('#product_price').data('price');
  $('.quantity_for_sum').each(function() {
    productPrice += +this.value * price;
  });
 });
 $('.total_count').html("Total: $"+productPrice);
});
});

Working copy - http://plnkr.co/edit/UfAl463VBqQBcdcXfMIP?p=preview

1 Comment

Select 1 from first select input -> It will give total as $180 which is wrong. Also explain how your code is different from the one originally posted..

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.