Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Javascript toggle element display

I have a website with a menu element in a header grid and when the page is first displayd the menu is visible. I have a hamburger element which I want to use to toggle the menu display when it is clicked.

The CSS for the menu is:

/*==================================================================*/
/* NAVIGATION */
.main-nav {
    grid-area: N;
    text-transform: uppercase;
}

/* Style the nav links */
.main-nav ul {
    display: grid;
    grid-template-columns: repeat(4,20vw);
}

.main-nav li {
    /* No bullets */
    text-align: center;
    list-style-type: none;
    min-width: 20vw;
}

.main-nav a {
    /* Not italic unlike all other a elements */
    text-decoration: none;
    font-style: normal;
}

The hanburger uses a javascript function:

/* Toggle between showing and hiding the navigation menu links when the user clicks on the hamburger menu / bar icon */
function toggleMenu() {
    let menu = document.getElementById("main-nav");

    if (menu.display == "none"){
        alert("Hidden");
        menu.style.display = "grid";
    }
    else {
        alert("Displayed");
        menu.style.display = "none";
    }
 
};

When the page loads, this function is used in the hamburger's click event listener:

 document.getElementById("hamburger").addEventListener("click", toggleMenu);

Now, what happens is this - the page loads, the menu is visible:

Page when loaded first

I click on the hamburger the alert tells me the menu is currently visible:

Alert showing displayed

Then the menu is hidden:

Page with hidden menu

However, if I now click the hamburger again I get a message telling me that the menu is displayed(!) and it does not re-appear:

Menu apparently displayed but not visible

So, what I don't understand is why the element's style.display status is incorrect when the menu is clearly display:none;?

Of course, this then means that the function fails to re-show the menu as the status check is incorrect.

If you need any more code let me know but as far as I can see all the code involved is in the question.

Thanks in advance,

Dermot

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

In the end it was a simple error of leaving out .style as per Simon K's comment:

if (menu.style.display == "none"){
        alert("Hidden");
        menu.style.display = "grid";
    }
    else {
        alert("Displayed");
        menu.style.display = "none";
    }

Checking for meun.display did nothing as there was nothing to check for!

Many thanks to Simon K for spotting that!

about 4 years ago · Juan Pablo Isaza Report

0

Try this instead!

function toggleMenu() {
  let menu = document.getElementById("main-nav");

  if (window.getComputedStyle(menu).display == "none") {
    alert("Hidden");
    menu.style.display = "grid";
  } else if (window.getComputedStyle(menu).display == "grid") {
    alert("Displayed");
    menu.style.display = "none";
  }
}
.main-nav {
    grid-area: N;
    text-transform: uppercase;
}

/* Style the nav links */
.main-nav ul {
    display: grid;
    grid-template-columns: repeat(4,20vw);
}

.main-nav li {
    /* No bullets */
    text-align: center;
    list-style-type: none;
    min-width: 20vw;
}

.main-nav a {
    /* Not italic unlike all other a elements */
    text-decoration: none;
    font-style: normal;
}
<div class="main-nav">
  <ul id="main-nav">
    <li><a class="hoverlink activemenu" href="index.html">home</a></li>
    <li><a class="hoverlink" href="shop.html">shop</a></li>
    <li><a class="hoverlink" href="notes.html">notes</a></li>
    <li><a class="hoverlink" href="info.html">info</a></li>
  </ul>
  <button onClick="toggleMenu()">☰</button>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!