This is the HTML
structure of my sidebar menu. Submenu is a collapsible menu.
<ul class="block-navigation" id="sidebar-navigation">
<li class="navigation-item has-child navigation-submenu">
<a class="open-submenu" href="#">
School Affairs
<em></em>
</a>
<ul class="block-navigation-submenu">
<li class="navigation-item">
<a class="" href="#">
<span>School Affairs</span>
</a>
</li>
<li class="navigation-item">
<a class="" href="#">
<span>National Schools</span>
</a>
</li>
<li class="navigation-item">
<a class="" href="#">
<span>Teacher Transfers</span>
</a>
</li>
</ul>
</li>
<li>
-----
-----
</li>
</ul>
My question is, I need to close all submenus except target one. Submenu triggering by clicking on the <em>
element inside parent <a>
This is how I tried it in jQuery
$("#sidebar-navigation li.navigation-submenu > a > em").on('click', function(e) {
e.preventDefault();
let $el = $(this).parent().parent('li');
$($el).not($el).removeClass("active");
$($el).not($el).find(".navigation-submenu").slideUp();
$(this).parent().parent('li').toggleClass("active");
$(this).parent().parent('li').find(".navigation-submenu").slideToggle();
});
But this only work to open submenu.. Hope some body may help me out.
2
Answers
$($el)
will select only one element$el
it self. See the selected elements length in my snippet console.You can use
siblings()
.Or class selector
Consider the following.
This closes the
active
list, removes the class, opens the target, and adds the class to it.