1

I am trying dynamic sum with JavaScript in my Laravel application. It is working fine when there is one row. But if I make a for each loop, how can I get the sum for each row?

Here is my form:

@foreach($users as $user)
    <div class="form-group">
        <input id="item1" type="number" class="prc form-control" name="item1[]">
    </div>
    <div class="form-group">
        <input id="item2" type="number" class="prc form-control" name="item2[]">
    </div>
    <output id="result" class="form-group"></output>
@endforeach  

My JavaScript code is


$('.form-group').on('input', '.prc', function(){
   let totalSum = 0;
    $('.form-group .prc').each(function(){
         const inputVal = $(this).val();
          if($.isNumeric(inputVal)){
               totalSum += parseFloat(inputVal);
           }
      });
    $('#result').text(totalSum);
 });

enter image description here

I am getting the result in the first row only. How can I get values row by row?

3
  • what is your expected result ?? 8 is correct? Commented Apr 28, 2021 at 13:32
  • @A.ANoman I expect 4 in each row. Commented Apr 28, 2021 at 13:36
  • You may try dynamic id and name like add extra foreach key and add this key with id and name Commented Apr 28, 2021 at 13:41

1 Answer 1

1

You have repeated ids so change them to class . Then , just add one outer div for per rows and use .closest(".outer") and .find(".prc") to loop through all inputs in that rows and then add total inside result using ..find(".result").text(totalSum)

Demo Code :

$('.form-group').on('input', '.prc', function() {
  let totalSum = 0;
  //get closest outer div
  var selector = $(this).closest(".outer")
  selector.find(".prc").each(function() {
    const inputVal = $(this).val();
    if ($.isNumeric(inputVal)) {
      totalSum += parseFloat(inputVal);
    }
  });
  //find result add result there
  selector.find(".result").text(totalSum);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--added outer div-->
<div class="outer">
  <div class="form-group">
    <!--use class-->
    <input type="number" class="prc  item1 form-control" name="item1[]">
  </div>
  <div class="form-group">
    <input type="number" class="prc  item2 form-control" name="item2[]">
  </div>
  <output class="result" class="form-group"></output>
</div>
<div class="outer">
  <div class="form-group">
    <input type="number" class="prc  item1 form-control" name="item1[]">
  </div>
  <div class="form-group">
    <input type="number" class="prc  item2 form-control" name="item2[]">
  </div>
  <output class="result" class="form-group"></output>
</div>

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

1 Comment

Glad i was able to help :)

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.