I have two parent items with sub-menus and have the code firing to open the current sub-menu while closing the other open ones.
But I can’t seem to get the .toggleClass()
to fire on the open menu to close it (I want to toggle a menu item open/close).
<ul>
<li class="menu-item-has-children">
<a href="#">Services</a>
<ul class="sub-menu">
<li><a href="#">Thing 1</a></li>
<li><a href="#">Thing 2</a></li>
</ul>
</li>
<li class="menu-item-has-children">
<a href="#">Services</a>
<ul class="sub-menu visible">
<li><a href="#">Widget 1</a></li>
<li><a href="#">Widget 2</a></li>
</ul>
</li>
</ul>
And here is my current jquery
// main menu toggle of sub-menu
$(".menu-item-has-children > a").click(function(e) {
// remove .visible from other .sub-menu
$(".sub-menu").removeClass('visible');
// toggle the .visible class on the current parent item
$(this).next(".sub-menu").toggleClass('visible');
// prevent the <a> from default behavior
e.preventDefault();
});
2
Answers
toggleClass is always do add class if use after removeClass
This doesn’t work, because
$(".sub-menu")
selects all sub-menus and removes thevisible
class from each. NexttoggleClass
adds again (toggle) the removedvisible
class.If you want to remove
visible
from the other elements only, you must go up the hierarchy and fetch all sibling elements from the parent. Nowvisible
is removed from the other sub-menu only: