Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sum table row values

$(function() {
  $("#subtotal").html(sumColumn(4));
  $("#total").html(sumColumn(5));
});

function sumColumn(index) {
  var total = 0;
  $("td:nth-child(" + index + ")").each(function() {
    total += parseInt($(this).text(), 10) || 0;
  });  
  return total;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table style="border-spacing: 10px;">
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>12</td>
    <td>34</td>
  </tr>
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>56</td>
    <td>78</td>
  </tr>
  <tr>
    <td>ClientName</td>
    <td>ItemName</td>
    <td>Quantity</td>
    <td>90</td>
    <td>12</td>
  </tr>
  <tr>
    <td colspan="3">Totals</td>
    <td id="subtotal"></td>
    <td id="total"></td>
  </tr>
</table>
Comment

PREVIOUS NEXT
Code Example
Javascript :: js char array to string 
Javascript :: js stairs algorithm 
Javascript :: merge 2 json objects js 
Javascript :: javascript Sum of a sequence 
Javascript :: sorting in javascript 
Javascript :: reducer in react example 
Javascript :: detect click outside react component 
Javascript :: what is JSON TREE 
Javascript :: descending order in objects in js 
Javascript :: array shuffle 
Javascript :: what is status 400 in react 
Javascript :: jquery has parent with class 
Javascript :: js iso date split 
Javascript :: js typeof number 
Javascript :: storing an image file into buffer field with mongoose 
Javascript :: axios patch 
Javascript :: react navigation history clear 
Javascript :: timeout 
Javascript :: nuxt eslint prettier vetur 
Javascript :: filter repetition 2d array javascript 
Javascript :: vuetify autocomplete get input value 
Javascript :: javascript string replace all 
Javascript :: drupal 9 get nid from node 
Javascript :: javascript prompt yes/no 
Javascript :: how to iterate array in javascript 
Javascript :: how to stop re rendering in react 
Javascript :: includes method javascript 
Javascript :: remove table line button html using javascript 
Javascript :: completely remove duplicate element from the array 
Javascript :: get localstorage value 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =