Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

282
Views
how i get the grand total using jquery each loop to total input field

I try to get the grand total using each loop but it return nothing

[![enter image description here][1]][1]

here is html code

<div class="main-wrapper">
    <div class="row">
        <div class="col-md-4">
            <label>quentity</label> 
            <input type="text" name="quantity[]" id="quantity">
        </div>
        <div class="col-md-4">
            <label>price</label>
            <input type="text" name="price[]" id="price">
        </div>
        <div class="col-md-4">
            <label>total</label>
            <input type="text" name="total[]" id="total">
            <a class="btn btn-danger btn1" onclick="add()">+</a>
        </div>
    </div>
    <div class="append">
        
    </div>
    <div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4"></div>        
    <div class="col-md-4 g-total"><label>gtotal</label><input type="text" name="g-total" id="gtotal"></div>
    </div>
    </div>

here is script code

$('.main-wrapper').on("change","#price",function(){
    var gtotal =0;
    var price = $(this).val();
    var quantity = $(this).parents('.row').find('#quantity').val();
    function multi(){
        var total = price * quantity;
        return total;
    }
     $(this).parents('.row').find('#total').val(multi());
     
})

var gtotal = 0;
$('#total').each(function(){
    gtotal += $(".main-wrapper").children().find('#total').val();
    console.log(gtotal);
})

enter image description here value in total comes from quality and price and its working but each loop on total not working Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This should work:

$('.main-wrapper').on("change", "#price", function() {
  var gtotal = 0;
  var price = $(this).val();
  var quantity = $(this).parents('.row').find('#quantity').val();

  function multi() {
    var total = price * quantity;
    return total;
  }
  $(this).parents('.row').find('#total').val(multi());

});

$("#getgrandtotalbtn").click(function() {
var gtotal = 0;
      $('.total').each(function() {
          gtotal += parseInt($(this).val());
        })
        $("#gtotal").val(gtotal);
      });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-wrapper">
  <div class="row">
    <div class="col-md-4">
      <label>quentity</label>
      <input type="text" name="quantity[]" id="quantity">
    </div>
    <div class="col-md-4">
      <label>price</label>
      <input type="text" name="price[]" id="price">
    </div>
    <div class="col-md-4">
      <label>total</label>
      <input type="text" name="total[]" class="total" id="total">
      <a class="btn btn-danger btn1" onclick="add()">+</a>
    </div>
  </div>
  <div class="row">
    <div class="col-md-4">
      <label>quentity</label>
      <input type="text" name="quantity[]" id="quantity">
    </div>
    <div class="col-md-4">
      <label>price</label>
      <input type="text" name="price[]" id="price">
    </div>
    <div class="col-md-4">
      <label>total</label>
      <input type="text" name="total[]" class="total" id="total">
      <a class="btn btn-danger btn1" onclick="add()">+</a>
    </div>
  </div>
  <div class="append">

  </div>
  <div class="row">
    <div class="col-md-4"></div>
    <div class="col-md-4"></div>
    <div class="col-md-4 g-total"><label>gtotal</label><input type="text" name="g-total" id="gtotal"></div>
  </div>
  <button id="getgrandtotalbtn">Calculate</button>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!