Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda