-1

I'm trying to make a nav bar show when a button(icon) is clicked and disappear when it is clicked again. The first part works where it shows but then when clicked again, nothing happens. I can't figure out why the second part is being ignored. Thank you

let nav = document.querySelector("nav");
let icon = document.querySelector(".mobile-icon");
console.log(nav);
icon.addEventListener("click", showMenu)

function showMenu() {
if (nav.style.display = "none"){
nav.style.display = "block";
} else {
    nav.style.display = "none";
}

}
JMay
  • 341
  • 1
  • 4
  • 10

1 Answers1

1

You need to use the equality operator === when comparing the display string value. You're currently using an assignment operator:

function showMenu() {
  if (nav.style.display === "none"){
    nav.style.display = "block";
  } else {
    nav.style.display = "none";
  }
}
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119