I've been recently trying to build a WordPress based website, and I added few JavaScripts modules to it and they all worked fine until today. I have no idea how it happened or how to solve it. I get an error reading:
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
My code:
class MobileMenu {
constructor() {
this.menu = document.querySelector(".site-header__menu")
this.openButton = document.querySelector(".site-header__menu-trigger")
this.events()
}
events() {
this.openButton.addEventListener("click", () => this.openMenu())
}
openMenu() {
this.openButton.classList.toggle("fa-bars")
this.openButton.classList.toggle("fa-window-close")
this.menu.classList.toggle("site-header__menu--active")
}
}
export default MobileMenu
I'd appreciate any help at this point, I'm fairly a beginner and I couldn't find any solution for the past few hours. Thanks!