Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda