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

261
Vistas
Trying to close popup by clicking outside of div breaks button that opened it

I have a mobile/responsive nav menu that is opened with a button using jquery. The button was working great until I added a jquery script to also close the window if the user clicks anywhere outside of the menu div, that script also works as intended to close the menu everywhere except when attempting to use the initial menu button, which won't function if I try to close the menu with it (but can still be used to open the menu again if its closed).

Anyone able to help me figure out why the button itself isnt working?

  $(document).ready(function() {
    var mobileNav = $('#mobile-nav');

    //Toggle hide on the menu
    $('.btn-navbar').on('click', function(){
      mobileNav.toggleClass('d-none');
    });

    // Hide menu if clicked outside
    $(document).mouseup(function(e){
      if (!mobileNav.is(e.target) && mobileNav.has(e.target).length === 0 && !mobileNav.hasClass('d-none')){
        $('#mobile-nav').toggleClass('d-none');
      }

    });
     
  });
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Since mouseup event fires before click event, so when you click on .btn-navbar actually first the handler related to mouseup runs, and it toggles the class d-none on mobileNav, after that the handler related to click runs, and it again toggles the d-none class on mobileNav, so as a result it's display never changes, to solve them problem,in the mouseup handler you can check if the target element is not .btn-navbar then toggle d-none class, like this:

$(document).mouseup(function(e){
  if (!$(e.target).is('.btn-navbar') && !mobileNav.is(e.target) && mobileNav.has(e.target).length === 0 && !mobileNav.hasClass('d-none')){
    $('#mobile-nav').toggleClass('d-none');
  }
});

Here is a simple example:

$(document).ready(function() {
  var mobileNav = $('#mobile-nav');

  //Toggle hide on the menu
  $('.btn-navbar').on('click', function() {
    mobileNav.toggleClass('d-none');
  });

  $(document).mouseup(function(e) {
    if (!$(e.target).is('.btn-navbar') && !mobileNav.is(e.target) && mobileNav.has(e.target).length === 0 && !mobileNav.hasClass('d-none')) {
      $('#mobile-nav').toggleClass('d-none');
    }
  });

});
.d-none {
  display: none
}

#mobile-nav {
  background: red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<button class="btn-navbar">Toggle nav</button>
<h1 id="mobile-nav" class="d-none">nav content</h1>

about 4 years ago · Santiago Trujillo Denunciar

0

i'm not certain of the why but the button doesn't like the $(document) declaration ... i'm now using the same function but using the id of the main content div

about 4 years ago · Santiago Trujillo 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