Home > Back-end >  update let value onclick
update let value onclick

Time:05-04

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" >
    <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  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

CodePudding user response:

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";
}
*/

CodePudding user response:

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" >
          <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  a href="index.html#Dona!"> Doná!</a>
          </div>
        </div>
      </div>

CodePudding user response:

You need to move the if-else block into the toggleMenu function so that every time you toggle the value of the variable, it will update the ui.

ALso the else block which hides certain ui elements need to be specified at start in order to not display the elements initially, failing to do so will result in both open and close rendering at the same time. thus you need to add the code below at the beginning too.

      //This block is to set initial visibility of the elements
      myDropdownMenu.style.display = "none";
      openNav.style.display = "flex";
      closeNav.style.display = "none";

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

      let mobileMenu = false;
      //This block is to set initial visibility of the elements
      myDropdownMenu.style.display = "none";
      openNav.style.display = "flex";
      closeNav.style.display = "none";
      ///////////////////////////////////
      function toggleMenu() {
        mobileMenu = !mobileMenu;
        console.log(mobileMenu);
        //This block is shifted into toggleMenu function as you need to update ui whenever the value is toggled
        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" >
        <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  a href="index.html#Dona!"> Doná!</a>
        </div>
      </div>
    </div>

CodePudding user response:

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.

  • Related