user3541457
user3541457

Reputation: 13

Can't Read property innerHTML Javascript

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

Answers (2)

epascarello
epascarello

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

ProllyGeek
ProllyGeek

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

Related Questions