I am working on a sidebar menu navigation which expands and collapses when user clicks on the menu. On top of that, I want the menu to stay expanded if the link on the sub menu is active when the page loads.
This is the code sample that I am working on right now. Can someone please help me check the issue on my code?
jQuery(document).ready(function(e) {
{
var m = e("ul.menu li.sidebar ul.sub-menu");
e("li")
}
e("ul.menu > li.sidebar > a").click(function(e) {
e.preventDefault()
}),
e(document).mouseup(function(i) {
var n = e("li.sidebar");
n.is(i.target) || 0 !== n.has(i.target).length || m.hide("fast")
});
});
.sidebar {
padding: 0 !important;
margin-top: 2%;
}
.sidebar a {
background-color: #122842;
padding: 3%;
color: white !important;
}
.sidebar .sidebar-submenu a {
color: #6e6e6e !important;
background-color: #e1ecf1 !important;
display: block;
}
.sidebar .current-menu-item a {
color: #22374f !important
}
.sidebar .sub-menu {
display: none;
}
.current-menu-ancestor .sub-menu {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<ul class="menu ">
<li class="sidebar current-menu-ancestor"><a href="#">Menu A ▶</a>
<ul class="sub-menu">
<li class="sidebar-submenu current-menu-item"><a href="a.com/menu1/">submenu 1</a>
</li>
<li class="sidebar-submenu current-menu-item"><a href="a.com/menu1/#sectionb">submenu 2</a>
</li>
<li class="sidebar-submenu"><a href="a.com/menu2">submenu 3</a>
</li>
</ul>
</li>
<li class="sidebar current-menu-ancestor"><a href="#">Menu B ▶</a>
<ul class="sub-menu">
<li class="sidebar-submenu current-menu-item"><a href="a.com/menu1/#sectionc">submenu 4</a>
</li>
<li class="sidebar-submenu"><a href="a.com/menu2/#sectionb">submenu 5</a>
</li>
<li class="sidebar-submenu"><a href="a.com/menu3">submenu 6</a>
</li>
</ul>
</li>
<li class="sidebar"><a href="#">Menu C ▶</a>
<ul class="sub-menu">
<li class="sidebar-submenu"><a href="a.com/menu4">submenu 7</a>
</li>
<li class="sidebar-submenu"><a href="a.com/menu5">submenu 8</a>
</li>
</ul>
</li>
</ul>
These are the behavior that I want to achieve:
- Submenu 1, submenu 2, and submenu 4 are on the same page but different 'menu'. If the page is selected, both menu A and menu B expand while menu C stay collapse.
- If user clicks on submenu 3 which is under menu A, when the page loads, menu A expands while menu B and C collapse.
- Whenever a submenu is active, class "current-menu-item" is added to the li tag and class "current-menu-ancestor" is added to the ul tag
The issue that I have right now:
- The menu is automatically expanded when any sub-menu is active, but the menu is not clickable
when I replace the jQuery with the code below, the menu is clickable and automatically expanded when any sub-menu is active, BUT the sub menu is not clickable (not opening the link)
jQuery(document).ready(function(e){ jQuery('.sidebar a').click(function(e){ e.preventDefault(); if (jQuery(('.sidebar').children('.sub-menu:first').is(':visible')) { jQuery(('.sidebar').children('.sub-menu:first').hide(); } else { jQuery(('.sidebar').children('.sub-menu:first').show(); }
}); });
Thank you in advance!