0

I'm trying to set up a function that calculates the total when two inputs are changed within an .item. I want this to happen for the inputs on each .item only, but currently the way I have set up my calculateTotal() function does not run for each .item on keyup

function calculateTotal() {
  $(".item").each(function() {
    var item = $(this);
    var qty = item.find(".qty").val();
    var price = item.find(".cost").val();
    var total = Number(qty) * Number(price);
    $(".totalprice").text(total);
  });
}
calculateTotal();
$(".input--item").keyup(function() {
  calculateTotal();
});
.item {
  display: flex;
  align-items: center;
}

.wrap {
  border: 2px solid #eee;
  display: flex;
  flex-direction: column
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="12" maxlength="3" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="55" maxlength="4" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="40" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="79" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>

1
  • 2
    $(".totalprice", this) <= you need to find the totalprice for each of the items. or item.find('.totalprice') since you already have a jQuery object for the item. Commented Jul 3, 2019 at 14:34

2 Answers 2

1

Use item.find(".totalprice").text(total)

function calculateTotal() {
  $(".item").each(function() {
    var item = $(this);
    var qty = item.find(".qty").val();
    var price = item.find(".cost").val();
    var total = Number(qty) * Number(price);
    item.find(".totalprice").text(total)
    
  });
}
calculateTotal();
$(document).keyup('.input--item',function() {
  calculateTotal();
});
.item {
  display: flex;
  align-items: center;
}

.wrap {
  border: 2px solid #eee;
  display: flex;
  flex-direction: column
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="12" maxlength="3" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="55" maxlength="4" />
    </div>
  </div>
 <span class="totalprice"></span><br>
</div>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="40" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="79" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>

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

Comments

0

Here you are changing text of all the divs, instead you have to change the div for the current item, do it like this

function calculateTotal() {
  $(".item").each(function() {
    var item = $(this);
    var qty = item.find(".qty").val();
    var price = item.find(".cost").val();
    var total = Number(qty) * Number(price);
    $(this).find(".totalprice").text(total);
  });
}

calculateTotal();
$(".input--item").on("input",function() {
  calculateTotal();
});
.item {
  display: flex;
  align-items: center;
}

.wrap {
  border: 2px solid #eee;
  display: flex;
  flex-direction: column
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="12" maxlength="3" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="55" maxlength="4" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>
<div class="item">
  <div class="wrap">
    Cost:
    <div class="edit">
      <input class="input--item qty" type="text" name="name" autocomplete="off" value="40" />
    </div>
    Price:
    <div class="edit">
      <input class="input--item cost" type="text" name="name" autocomplete="off" value="79" />
    </div>
  </div>
  <span class="totalprice"></span><br>
</div>

Comments

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.