Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

J Query Toggle sub menus not working as desired

I have a function that toggles a sub-menu when the parent is clicked.  This works fine.  However, if I click on another menu choice (parent) the sub-menu toggles open but the prior sub-menu that was toggled open stays open.  What I want is for each toggled sub-men closes if another parent is clicked so only one sub-menu is open at a time.

$("#menu-mobile .sub-menu").hide();
   
$('#menu-mobile').children().click(function(){
    $(this).children('.sub-menu').slideToggle('slow'); 
});

Open in new window

JavaScriptjQuery

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
Zvonko

You mean this:


$("#menu-mobile .sub-menu").hide(); 
    
$('#menu-mobile').children().click(function(){ 
   $("#menu-mobile .sub-menu").hide(); 
   $(this).children('.sub-menu').slideToggle('slow');  
});

Open in new window

ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck