JavaScript JavaScript and the DOM Getting a Handle on the DOM Select a Page Element By Its ID

Landon SooHoo
Landon SooHoo
1,307 Points

It throws this error: Uncaught TypeError: Cannot read property 'addEventListener' of null at app.js:4

I looked at the HTML very carefully and there was not any errors. But it still says: Uncaught TypeError: Cannot read property 'addEventListener' of null at app.js:4

Brandon White
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

Hi Lucas,

You’d really need to post your code in order for someone to offer the best advice, as it’s difficult to debug without being able to test code.

That said, based on the error and a review of the video, I’m guessing that your myButton variable (I’m assuming you’re calling addEventListener on myButton) doesn’t actually hold a value. This would happen if the element you’re trying to select can’t be found in the DOM.

Try logging out whatever variable you’re calling addEventListener on to see if it holds a value.

1 Answer

Landon SooHoo
Landon SooHoo
1,307 Points

Thank you. I figured out what the problem was; I put the script tags after the HTML.