I'm trying to make a menu that collapses, markup is below:
Whenever a .cat_menu_header is clicked, the ul below it should expand and all expanded ul's should collapse. So we start with everything collapsed and only the .cat_menu_header elements visible, with max 1 .submenu being expanded at one time.
<ul id='sidebarNav'>
<li class='cat_menu_header'><a class='collapsed' href='#'>Link 1
<ul class='submenu'>
<li><a href='#'>text 1</a></li>
<li><a href='#'>text 2</a></li>
</ul>
</li>
<li class='cat_menu_header'><a class='collapsed' href='#'>Link 2
<ul class='submenu'>
<li><a href='#'>text 3</a></li>
<li><a href='#'>text 4</a></li>
</ul>
</li>
</ul>
The follow js makes each .submenu expand and collapse but I can't figure out how to select all expanded ul's and collapse them. I added the .collapsed class which could be changed to .expanded with toggleClass.
I know I'm close ...
$(document).ready(function(){
//sidebar collapsible menu
$("#sidebarNav > li > ul").hide();
$("#sidebarNav a.collapsed").click(function() {
$(this).toggleClass("expanded").toggleClass("collapsed").find("+ ul").slideToggle("fast");
return false;
});
});