-2

I want to add the data of some data-attributes within a simple formula. But the values don't get interpreted as numbers, but as strings. I do it like that:

 var nutzer = document.getElementById('chosen_meals').getElementsByTagName('p');
 var kalorien = 0;
 for (var i = 0; i < amountOfMeals_low; i++) {
     var kalorien = kalorien + nutzer[i].dataset.kal;
     console.log(kalorien);
 }

And the markup is some like that

<ul id="chosen_meals">
  <li>
    <p data-kal="420">Foo</p>
  </li>
  <li>
    <p data-kal="180">Foo</p>
  </li>
</ul>

And I do get logged 0 0420 0420180

How would I get it to be the result of 600?

terrymorse
  • 6,771
  • 1
  • 21
  • 27
Daiaiai
  • 1,019
  • 3
  • 12
  • 28

1 Answers1

2

try to convert it using Number or parseInt
e.g. Number(nutzer[i].dataset.kal)

hhh
  • 97
  • 6