Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!