Reputation: 13
I'm getting the following error: cannot read property 'innerHTML' of null.
Here is the HTML part:
<div id="itemcost0">300</div>
Now the Javascript part:
itemcount = parseInt(document.getElementById("itemcounter").value);
var loop = 0;
while(loop<=itemcount)
{
itemprice = parseInt(document.getElementById("itemcost" + loop).innerHTML);
loop++;
}
Before anyone suggests this, the javascript is located after the table the div itemcost0 is in. It should also be noted that itemcount has the correct value.
Upvotes: 1
Views: 85
Reputation: 207501
You are using zero index, so the count is probably one less than the max so
while(loop<=itemcount)
should probably be
while(loop<itemcount)
Upvotes: 6
Reputation: 15836
itemcount = parseInt(document.getElementById("itemcounter").value);
var loop = 0;
while(loop<=itemcount)
{
itemprice = parseInt(document.getElementById("itemcost" + loop.toString()).innerHTML);
loop++;
}
loop should be a string not an integer.
Upvotes: 0