on-internet
← Community

Cannot read property 'addEventListener' of null

Stack Overflow ·

I have to use vanilla JavaScript for a project. I have a few functions, one of which is a button that opens a menu. It works on pages where the target id exists, but causes an error on pages where the id doesn't exist. On those pages where the function cannot find the id, I receive a "Cannot read property 'addEventListener' of null " error and none of my other functions work. Below is the code for the button that opens the menu. function swapper() { toggleClass(document.getElementById('overlay'), 'open'); } var el = document.getElementById('overlayBtn'); el.addEventListener('click', swapper, f

Comments

Sign in or sign up to comment and upvote.

No comments yet.