Enclosed within span tags, the following values are present:
<div id="aantallen">
<span>3</span>
<span>2</span>
</div>
To find the sum of these values in JavaScript, a For Loop was created:
var div = document.getElementById("aantallen");
var spans = div.getElementsByTagName("span");
for(i=0;i<spans.length;i++)
{
var totalPersons = totalPersons + i;
alert(totalPersons);
}
Since 3 and 2 are considered as text, the alerts display NaN. The question posed is: How can one convert 3 and 2 into strings in order to effectively calculate their sum?
A solution attempted involved using String(i)
, but it failed to yield the desired outcome.