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

136
Vistas
When toggling a class on a menu element. How can I make it so that class gets removed on previously toggled elements?

I have this menu with different submenu's which are just <ul>. When clicking on a submenu the .active class will get toggled and the submenu will display. However when I click on a different submenu the desired behaviour is that the .active class will be removed from the previous submenu and applied to the new one. However what obviously happens now is that the new submenu will also get the .active class and both menu's will be open at the same time which is not what I'm after.

What would be a simple way to make sure the .active class gets removed when I click the next menu item?

This is my current code

$(".menu nav ul li > ul.subnav li").on("click", function (_event) {
  $(this).children("ul.subnav").toggleClass("active");
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Remove .active from the other active elements and add it for this element.

$(".menu nav ul li > ul.subnav li").on("click", function (_event) {
  $(".menu nav ul li > ul.subnav li ul.subnav.active").removeClass("active");
  $(this).children("ul.subnav").addClass("active");
});
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