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

115
Vistas
How do i toggle something on click using an event listener if condition is met?

I want to toggle a navbar on click using a button and if the navbar is already active I want to disable it when the user clicks anywhere. The event listeners are all connected and the toggling code works normally but implementing the "click anywhere on the screen to turn off" feature isn't working.

Edit: I can now toggle by clicking anywhere but I can't toggle using the button I can only turn it on and not off.

const button = document.querySelector('button.mobile-menu-button');
const menu = document.querySelector('.mobile-menu');

let menuActive = false;

button.addEventListener('click', (e) => {
    e.stopPropagation();
    if (!menuActive) {
        menu.classList.toggle('hidden');
        menuActive = true;
    }
});

window.addEventListener('click', () => {
    if (menuActive) {
        menu.classList.toggle('hidden');
        menuActive = false;
    }
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The problem is that when you click on the button, you are also clicking on the window at the same time. You should use stopProgopation().

Here is a solution. When the button is clicked, the menu gets shown. If the user then clicks on the screen somewhere else other than the button, the menu will disappear.

<div id="menu" class="mobile-menu hidden">This is the menu</div>
<button class="mobile-menu-button">Button</button>

<script>
    const button = document.querySelector('button.mobile-menu-button');
    const menu = document.getElementById('menu');

    let menuActive = false;

    button.addEventListener('click', (e) => {
        e.stopPropagation();
        if (!menuActive) {
            menu.classList.toggle('hidden');
            menuActive = true;
        }
    });

    window.addEventListener('click', () => {
        if (menuActive) {
            menu.classList.toggle('hidden');
            menuActive = false;
        }
    });
</script>

<style>
    .mobile-menu {
        display: block;
    }

    .hidden {
        display: none;
    }
</style>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found I didn't need a condition for the button itself, only the window. Thanks to everyone for helping.

const button = document.querySelector('button.mobile-menu-button');
const menu = document.querySelector('.mobile-menu');

let menuActive = false;

button.addEventListener('click', (e) => {
    e.stopPropagation()
    menu.classList.toggle('hidden');
    menuActive = true;
});


window.addEventListener('click', () => {
    if (menuActive) {
        menu.classList.toggle('hidden');
        menuActive = false;
    }
});
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