2

I want to sum of multiple numbers but this is unable to work with each function.

What is tried:-

totalPgCost();
function totalPgCost(){
var totalPgCost = 0;
$('.pgBookingTable tr').find('.pgCost').each(function(){
totalPgCost += $(this).val();
});
$('.totalPgCost').text(totalPgCost);
                    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<table class="pgBookingTable">
<tr><td><span class="pgCost">10000</span></td><tr>
<tr><td><span class="pgCost">5000</span></td><tr>
<tr><td>Total <span class="totalPgCost"></span> /-</td><tr>
</table>

Why i'm getting 0?

Answer will appreciated!

4 Answers 4

3

You need text not val. Also the html table is not properly balanced, as there was no closing tr. Before adding the convert the string to number

totalPgCost();

function totalPgCost() {
  var totalPgCost = 0;
  $('.pgBookingTable tr').find('.pgCost').each(function() {
    totalPgCost += parseInt($(this).text().trim(), 10);
  });
  $('.totalPgCost').text(totalPgCost);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<table class="pgBookingTable">
  <tr>
    <td><span class="pgCost">10000</span></td>
  </tr>
  <tr>
    <td><span class="pgCost">5000</span></td>
  </tr>
  <tr>
    <td>Total <span class="totalPgCost"></span> /-</td>
  </tr>
</table>

You can also avoid find if you are sure that only span will inside thar table will have that class

totalPgCost();

function totalPgCost() {
  var totalPgCost = 0;
  $('.pgBookingTable .pgCost').each(function() {
    totalPgCost += parseInt($(this).text(), 10);
  });
  $('.totalPgCost').text(totalPgCost);

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<table class="pgBookingTable">
  <tr>
    <td><span class="pgCost">10000</span></td>
  </tr>
  <tr>
    <td><span class="pgCost">5000</span></td>
  </tr>
  <tr>
    <td>Total <span class="totalPgCost"></span> /-</td>
  </tr>
</table>

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

Comments

2

Use .text

$('.pgBookingTable tr').find('.pgCost').each(function(){
   totalPgCost += parseInt($(this).text());
});

Comments

0

 totalPgCost();
    function totalPgCost(){
    var totalPgCost = 0;
    $('.pgBookingTable tr').find('.pgCost').each(function(){
      totalPgCost += parseInt($(this).text());
    });
    $('.totalPgCost').text(totalPgCost);
                        }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


    <table class="pgBookingTable">
    <tr><td><span class="pgCost">10000</span></td><tr>
    <tr><td><span class="pgCost">5000</span></td><tr>
    <tr><td>Total <span class="totalPgCost"></span> /-</td><tr>
    </table>

Comments

0

you can directly use pgCost for each loop and use text() instead of val() to get the text within span hope this helps

totalPgCost();
function totalPgCost(){
var totalPgCost = 0;
$('.pgCost').each(function(){
totalPgCost += parseInt($(this).text());
});
$('.totalPgCost').text(totalPgCost);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<table class="pgBookingTable">
<tr><td><span class="pgCost">10000</span></td><tr>
<tr><td><span class="pgCost">5000</span></td><tr>
<tr><td>Total <span class="totalPgCost"></span> /-</td><tr>
</table>

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.