8

I have a html that contains tables like the following example

<td class="topiccell">
    <span class="topicnormal">
        <a class="value" href="/topic/?lang=en&action=viewtopic&topic=http%3A%2F%2Fwww.wandora.org%2Fsparql%2Fresultset%2Fliteral%2F40">
                 40
        </a>
    </span>
</td>
<td class="topiccell">
   <span class="topicnormal">
       <a class="value" href="/topic/?lang=en&action=viewtopic&topic=http%3A%2F%2Fwww.wandora.org%2Fsparql%2Fresultset%2Fliteral%2F40">
                 3
        </a>
   </span>
</td>

and I need to parse 40, 3 and another 75 numbers using .innerHTML. Then I would like to make a sum of all 75 numbers. I used the following

var valuelements = document.getElementsByClassName("value");
var features = new Array(valuelements.length);
for (var i=0; i<=features.length; i++){ 
  var val = valuelements[i].innerHTML;
  var counter = counter + val;
}
document.write(counter); 

and the result was like 40 3 etc.... tried parseInt, parseFloat, .value but the result always was NaN. Any suggestions?

2
  • you could try: var val = Number(valuelements[i].innerHTML); or var val = parseInt(valuelements[i].innerHTML); and declare your var counter outside the loop Commented Jan 31, 2013 at 2:00
  • 1
    1*s is your friend. If s is a string representation of a number, 1*s is a number. Also javascript does not have block scope variables, so declare all vars outside the for block. Commented Jan 31, 2013 at 2:12

2 Answers 2

13

You need to initialize counter with a starting number, otherwise you're performing math on undefined.

var counter = 0;

And then in the loop, use parseInt, parseFloat, or a direct number conversion on the .innerHTML value.

var counter = 0;

for (var i=0; i<features.length; i++){ 
  counter += parseFloat(valuelements[i].innerHTML);
}

In a modern browser, you could do this:

var count = [].reduce.call(valueelements, function(c, v) {
    return c + parseFloat(v.innerHTML);
}, 0);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your answers!Ok, tried all your answers and now is telling me that "Error:valuelements[i] is undefined" . I tried to make new Array(valuelements.length) but keeps telling is undefined... How to fix it?
@user2027553: I didn't notice you had <= in your for loop. It should just be <, as in i<features.length. I'll update.
3

Try something like this, using a more functional approach:

var sum = [].map.call(valuelements, function(v) {
  return +v.textContent; }).reduce(function(a, b) { return a + b; });

2 Comments

Just a note... Even though you're pre-mapping the elements to numbers, you should still provide a seed for .reduce just in case there are no valuelements found. With no seed, .reduce will throw an error when called on an empty Array. Ex: [].map(Number).reduce(Number); // TypeError ... [].map(Number).reduce(Number, 0); // 0
Yeah, I guess that would be good practice. I assumed OP has elements.

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.