4

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.

2
  • 4
    Woah, that's a ton of non-breaking spaces. Dude, you should look into css padding/margins/etc for styling. Commented Oct 31, 2017 at 14:33
  • Aside from that, if you put a span element around all the numbers you want to sum with a common class, then it's a simple operation to select all those, parse those into numbers, and then sum them. Commented Oct 31, 2017 at 14:36

2 Answers 2

5

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>

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

1 Comment

Marco, thank you .but above will be like a hard coded for a table with 3 column. I want to use same jq script to add total for any number of column.can you suggest please?
2

There were a few errors in your code. Here is the working version : http://jsfiddle.net/2q06sv8w/3/

$(document).ready(function () {
            var expenses = $('.expenses');
            var expenseTotal = $('#expenses_sum');
            expenseTotal.html('0');
            $.each(expenses, function (index, object) {
                var boldTag = $(object).find('b');
                if (boldTag && boldTag.length > 0 && $(boldTag[0]).html() != '') {
                    expenseTotal.html(parseInt(expenseTotal.html()) + parseInt($(boldTag[0]).html().replace(/,/g, '')));
                }
            })
        });
  1. The selector '.' is used to select elements matching a class name and '#' is used to select an element matching an id. So 'expenses_sum' must be an id and not a class.
  2. You should use html() and not val() to get and set the content of a td tag.
  3. The td tag contains the value inside a bold tag so you must use the html() on the bold tag.
  4. Declaring global variables and using $ prefix for variables does not look necessary here.

6 Comments

thanks acnn for such a nice explanation with logic.jsfiddle.net/7vd6bx4k does not works though.
I gave you the solution anyway
@acnn. Hi, i checked your fiddle answer, but comma seperated values are not added
@ Acnn thanks. total should be - 149677 but your update returns 1378.
@sana, you are wright, the comma seperated values are not adding
|

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.