Home > Software engineering >  Trying to use javascript with toggle to open navbar /hamburger menu with simple js, but says error o
Trying to use javascript with toggle to open navbar /hamburger menu with simple js, but says error o

Time:01-10

I got an assignment due in 2 days, where its required to use javascript, and I'm trying to get this navbar turning into a burger-menu when its on mobile-view with js to work (needs to be responsive). I found this simple code, and it worked until the last 2 lines...


const burgerMenu = document.querySelector(".burger_menu");
const navLinks = document.querySelector(".nav-links");

burgerMenu.addEventListener("click", () => {

  
  navLinks.classList.toggle(".mobile-menu");
});

It states this, when I open Inspect => Console: "Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at nav.js:11:12"

This works on this French guy's video its from, but I wonder if its the code thats wrong? typo I'm not seeing?

Any suggestions? any help would be much appreciated!

  • Related