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

160
Views
update let value onclick

I'm trying to update a boolean value onclick without any luck.

const myDropdownMenu = document.getElementById("dropdownMenu");
const openNav = document.getElementById("openNav");
const closeNav = document.getElementById("closeNav");

let mobileMenu = false;

function toggleMenu() {
  mobileMenu = !mobileMenu;
}

if (mobileMenu) {
  myDropdownMenu.style.display = "flex";
  closeNav.style.display = "flex";
  openNav.style.display = "none";
} else {
  myDropdownMenu.style.display = "none";
  openNav.style.display = "flex";
  closeNav.style.display = "none";
}
<div>
  <a href="index.html">
    <h1>CAVVD</h1>
  </a>
  <div id="mobileNav" class="mobileNavigation">
    <button id="openNav" onclick="toggleMenu();">
            Open
          </button>
    <button id="closeNav" onclick="toggleMenu();">
            Close
          </button>
    <div id="dropdownMenu">
      <a href="index.html#sobreNosotros">Sobre Nosotros</a>
      <a href="index.html#reuniones">Reuniones</a>
      <a href="ForodeConsultas.html">Foro de Consultas</a>
      <a href="index.html#HaceteVoluntario">Hacete Voluntario</a>
      <a href="Fotos.html">Fotos</a>
      <a href="index.html#contacto">Contacto</a>
      <a class="Dona" a href="index.html#Dona!"> Doná!</a>
    </div>
  </div>
</div>

Everything is working except the let mobileMenu, that is not updating with the toggleMenu() function. But if I return a simple console.log or alert, it works.

Can anybody help me? thanks

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

0

Your mobileMenu variable is working, the functionality not working is UI changes depend on mobileMenu variable. Modify your code to this:

const myDropdownMenu = document.getElementById("dropdownMenu");
const openNav = document.getElementById("openNav");
const closeNav = document.getElementById("closeNav");

let mobileMenu = false;

function toggleMenu() {
  mobileMenu = !mobileMenu;
  if (mobileMenu) {
    myDropdownMenu.style.display = "flex";
    closeNav.style.display = "flex";
    openNav.style.display = "none";
  } else {
    myDropdownMenu.style.display = "none";
    openNav.style.display = "flex";
    closeNav.style.display = "none";
  }
}

/*
if (mobileMenu) {
  myDropdownMenu.style.display = "flex";
  closeNav.style.display = "flex";
  openNav.style.display = "none";
} else {
  myDropdownMenu.style.display = "none";
  openNav.style.display = "flex";
  closeNav.style.display = "none";
}
*/
about 4 years ago · Juan Pablo Isaza Report

0

The if/else should be within the function, like this:

function toggleMenu() {
    mobileMenu = !mobileMenu;

    if (mobileMenu) {
        myDropdownMenu.style.display = "flex";
        closeNav.style.display = "flex";
        openNav.style.display = "none";
    } else {
        myDropdownMenu.style.display = "none";
        openNav.style.display = "flex";
        closeNav.style.display = "none";
    }
}

This is because otherwise the if/else only runs once (on load), whereas we want it to run every time the function is triggered.

about 4 years ago · Juan Pablo Isaza Report

0

Your if/else is in the wrong place so it doesn't execute when the function is executed. In order for the if/else to execute when the function is executed, it has to be in the curly brackets of the toggleMenu function (inside the function).

The code below fixed you problem by moving the if/else statement into the right place (into the function) so it executes when the function is called.

const myDropdownMenu = document.getElementById("dropdownMenu");
const openNav = document.getElementById("openNav");
const closeNav = document.getElementById("closeNav");

let mobileMenu = false;

function toggleMenu() {
  mobileMenu = !mobileMenu;

  if (mobileMenu) {
    myDropdownMenu.style.display = "flex";
    closeNav.style.display = "flex";
    openNav.style.display = "none";
  } else {
    myDropdownMenu.style.display = "none";
    openNav.style.display = "flex";
    closeNav.style.display = "none";
  }
}
      <div>
        <a href="index.html"> <h1>CAVVD</h1> </a>
        <div id="mobileNav" class="mobileNavigation">
          <button id="openNav" onclick="toggleMenu();">
            Open
          </button>
          <button id="closeNav" onclick="toggleMenu();">
            Close
          </button>
          <div id="dropdownMenu">
            <a href="index.html#sobreNosotros">Sobre Nosotros</a>
            <a href="index.html#reuniones">Reuniones</a>
            <a href="ForodeConsultas.html">Foro de Consultas</a>
            <a href="index.html#HaceteVoluntario">Hacete Voluntario</a>
            <a href="Fotos.html">Fotos</a>
            <a href="index.html#contacto">Contacto</a>
            <a class="Dona" a href="index.html#Dona!"> Doná!</a>
          </div>
        </div>
      </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!