DEV Community

Discussion on: Let's build a responsive navbar and hamburger menu using HTML, CSS, and Javascript.

Collapse
 
ryanstotler profile image
RyanStotler

This works great in codepen, I am trying to get the js to work and I am getting:
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
at site.js:165
from:
hamburger.addEventListener("click", mobileMenu);
Do you know how to resolve this property null in the javascript file?
Appreciate your help in advance

Collapse
 
iancahn profile image
Ian Cahn

Hey Ryan, I had this exact same issue, so Im gonna go ahead and guess you are using Chrome. Chrome has issues when using JS in localhost environment. To fix this, just move your js script tag to your footer, instead of your header. This fixed it for me, strange but hey...

Collapse
 
srichmond3000 profile image
Steve Richmond

Another solution is to add the 'defer' attribute to the script element.