document.getElementByID("test").innerHTML giving TypeError: 'undefined' is not a function (evaluating 'document.getElementByID("test")')
Asked Answered
O

3

8

I am trying to use javascript to set the inner html of a div, but for some reason, it isn't working. I have found that others have had this problem before, but none of the solutions I found in other posts works. I don't understand what's wrong.

Here is my test function:

function test(){
    document.getElementByID("test").innerHTML = "why won't you work";
    alert("hello");
}
window.onload = test;

The function is being called because the alert box works if the document.getElementByID line is commented out. It doesn't work if that line isn't commented. My console is showing an error for that line:

TypeError: 'undefined' is not a function (evaluating 'document.getElementByID("test")')

And of course, I have a div with that id in the body of my page.

<div id="test"></div>

Everything I have found online says that this should work. I am lost. Any help would be greatly appreciated.

Oletaoletha answered 17/11, 2013 at 7:39 Comment(0)
M
20

It's document.getElementById and not document.getElementByID.

You can test this right now using firebug or chrome dev tools on this site by doing:

document.getElementById('hlogo').innerHTML='It works!'; // have a look at the logo
Marquesan answered 17/11, 2013 at 7:41 Comment(4)
Oh my god! I spent the last hour and a half researching this. I could have sworn I saw people using ID. That was the problem. Thank you so much!Oletaoletha
If you use a plain text editor to write javascript I suggest you move to something better because it can save you from typos such as this one.Marquesan
No, I use windows. But it shouldn't be hard to find what people use nowadays.Marquesan
Things typo makes us do.Predominate
C
4

JavaScript is case sensitive. Watch your capitalization closely when you write JavaScript statements:

A function getElementById is not the same as getElementbyID.

A variable named myVariable is not the same as MyVariable.

Canotas answered 17/11, 2013 at 7:46 Comment(0)
C
1

I had this error because I was attempting to run document.getElementById('donationContainer').innerHTML('<div></div>');

instead of:

document.getElementById('donationContainer').innerHTML = '<div></div>';

Note the brackets: use ...innerHTML = ... not ...innerHTML(...

I hope this helps someone else fix this stupid issue

Cerenkov answered 30/7, 2020 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.