Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum all values for table column on html page

Tags:

html

jquery

I have a sample table look like this and data refreshed every 5 minute to show latest.example -

TollFree                    US                      Canadian                      
7,312                       826                     141,128     

I need to add and calculate total of above all.

 Total Records -

This is what i have tried so far- http://jsfiddle.net/unKDk/2666/

HTML page has 6 tables which has different number of columns to do same calculation. Trying to use same script in all as td class is same.

like image 820
sana Avatar asked Oct 23 '25 04:10

sana


1 Answers

Here we go:

var summ = Number($("td:nth-child(1)").text()) + Number($("td:nth-child(2)").text()) + Number($("td:nth-child(3)").text());
console.log("Result: " + summ);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>A</th>
<th>B</th>
<th>V</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>

Edit for your request:

var summ = 0;
$("td").each(function() {
  summ += Number($(this).text());
});;
console.log("Result: " + summ);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>A</th>
<th>B</th>
<th>V</th>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
like image 180
Marco Salerno Avatar answered Oct 25 '25 17:10

Marco Salerno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!