how to find sum of 2 columns in html table using jquery

Posted by Klbaiju under jQuery on 11/18/2016 | Points: 10 | Views : 367 | Status : [Member] | Replies : 1
Hi following is my html table

<table id="grdtable">

its output is as follows

Charge start End No Total
7 100 200 100 700
8 10 100 90 720
9 20 50 30 270

my requiremt is on the click event of button i want add the the result in a variable using each function
var str="";

just like

str= 7 * 100=700

means multiplying first column with fourth column

how to do this




Posted by: Manicse on: 11/18/2016 [Member] Bronze | Points: 25


We need to understand the Nodes that which are going to add up like First, Last,Last Child or First child something like.
Once you understand the basic logic in which we are going to perform he operation we can do with the following code as an example.

$("#sum_table tr:last td:not(:first)").text(function(i){

var t = 0;
t += parseInt( $(this).text(), 10 ) || 0;
return "Total: " + t;

In the above example I have taken last values for adding. So based on your requirement you should change the logic.


Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response