0

I have a table in an HTML file set up as follows (this is only one line of the table)

    <tr>
   <td class = "numeric">0</td>
   <td class = "numeric">2</td>
   <td class = "numeric">3</td>
   <td class = "numeric">1</td>
   <td class = "numeric">4</td>
   <td class = "numeric" id="city3"> </td>
      </tr>

and I need to write a script that will retrieve the numbers in the table so that I can total them up. Suffice to say I am completely stuck with how to retrieve the information. Any help is appreciated.

7
  • developer.mozilla.org/en-US/docs/Web/API/… Commented May 28, 2014 at 23:11
  • Is there only going to be numbers in the cells? Commented May 28, 2014 at 23:15
  • 1
    Where is your code, what have you tried? You can't just go "I tried and nothing worked, please write my code for me." Commented May 28, 2014 at 23:36
  • 1
    I'll just point out none of the answers below are using parseInt correctly: stackoverflow.com/questions/460172/… Commented May 28, 2014 at 23:56
  • 1
    @sjkm, no you are not. The Radix parameter should always be specified, please see: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Nothing to do with floating point. Commented May 29, 2014 at 0:02

5 Answers 5

2

if you want to achieve it using Native JavaScript (http://jsfiddle.net/S4XX2/1/)

var tds = document.getElementsByClassName('numeric');

var total = 0;

for(var i = 0; i < tds.length; i++) {
    var textValue = tds[i].innerHTML;
    if(textValue != '') {
        total += parseInt(textValue);   
    }
}

alert(total); // alerts the total sumed up

if you are using JQuery (http://jsfiddle.net/S4XX2/2/)

var total = 0;

$('td.numeric').each(function() {
    var textValue = $(this).text();
    if(textValue != '') {
        total += parseInt(textValue);  
    }
});

alert(total); // alerts the total sumed up
Sign up to request clarification or add additional context in comments.

1 Comment

You need to check for NaN as he has a space in his TD.
1

You will need to check for numbers (ints) as one of your tds is empty it will return an empty string or with the parseInt function it will return NaN. This will break any calculations you do later on.

The following requires jQuery but can easily be done with pure JS. It will check to see if it's a number and if not it will/should return 0 for those that aren't numbers and convert the 'string' numbers to actual ints for adding:

var total = 0;

$('td').each(function() {
    var num = parseInt(this.innerHTML, 10);  // Converts string to int. Added Radix as @JonP kindly pointed out.
    if (!isNaN(num)) {                       // Checks if all int are numbers and not NaN (not a number)
        total = total + num;                 // Just for clarity's sake this is written out fully
    }
});

alert(total);

Non jQuery way is this:

var rows = document.getElementsByClassName('numeric'),
    total = 0;

for(var i = 0; i < rows.length; i++) {
    var num = parseInt(rows[i].innerHTML, 10);
    if (!isNaN(num)) {
        total = total + num;
    }
}

alert(total);

EDIT: As @jonP pointed out if using parseInt(num, 10) you need to add a radix - which is the second parameter. Link to article explaining Radix. So line should read parseInt(this.innerHTML, 10)

2 Comments

You only need to check for NaN if there's a cell that contains a space. Please request a change in my code rather than just copying the answer.
It's not your code - I wrote it separately to what you posted, though I followed your helpful guidelines of posting a pure js version. He does have a space in his td.
0

A more compact way using native js functions instead of looping :

    var nums = document.getElementsByClassName('numeric');


 var total = nums.reduce(function(previousValue, currentValue, index, array){
   return previousValue + parseInt(array[index].innerHTML);
},0);

Comments

0
var items = document.getElementsByClassName('numeric');

var total = 0;

for (i = 0; i < items.length; i++) {
    var num = parseInt(items[i].innerHTML, 10) || 0; // converts to a number or zero if empty.
    total += num;   
}

document.getElementById('result').innerHTML = total;

Fiddle: http://jsfiddle.net/XJ5gS/

2 Comments

Your fiddle seems to be quite diferent from your actual answer.
oops - not sure what happened there - edited to reflect
-1

something like this:

var total;
$('td').each.(function() {
     total += $(this).val('td');
});

1 Comment

You need to mention that that requires jQuery, and your answer would be better for describing how to do it with just standard DOM accesses. (You may want to also suggest changes to the HTML to make it easier to process, such as adding an id attribute to the <tr>…)

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.