1

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.

2
  • 2
    What is this? document.getElementById("itemcounter").value can you add that part of the html? Commented Apr 16, 2014 at 13:59
  • 2
    Still might be a good idea to wrap this in a function and call it when the document is ready, no? Commented Apr 16, 2014 at 14:00

2 Answers 2

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

nice, no one found this :P
You were correct. I forgot to subtract 1 from itemcount. Thanks for the help.
0
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.

1 Comment

String + Interger = String AKA "itemcost" + 0 === "itemcost0"

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.