question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Active menu item with multi level submenus

See original GitHub issue

Hi, I’m using this code to add active class depending of the page.

This is working ok except for multi level submenus any clue how to fix this


/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.sidebar-menu a').filter(function() {
	 return this.href == url;
}).parent().addClass('active');

// for treeview
$('ul.treeview-menu a').filter(function() {
	 return this.href == url;
}).closest('.treeview').addClass('active');


<li class="treeview">
	<a href="#">
	      <i class="fa fa-gears"></i>
	      <span>Settings</span>
		<i class="fa fa-angle-left pull-right"></i>
	</a>
		<ul class="treeview-menu">
			<li>
				<a href="#">
				      <i class="fa fa-bars"></i>
					Specials Settings
				</a>
				<ul class="treeview-menu">
					<li><a href="setting1.php">Setting 1</a></li>
				</ul>
			</li>
		</ul>
</li>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:34

github_iconTop GitHub Comments

44reactions
REJackcommented, May 10, 2017
  1. nice script but it is not valid
  2. where you took the class ul.level-1 from? 😄 AdminLTE doesn’t use something like this.

i fixed it to get it work on AdminLTE

/** add active class and stay opened when selected */
var url = window.location;

// for sidebar menu entirely but not cover treeview
$('ul.sidebar-menu a').filter(function() {
	 return this.href == url;
}).parent().addClass('active');

// for treeview
$('ul.treeview-menu a').filter(function() {
	 return this.href == url;
}).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');

i tested all variations till level 3 (all items) 😃

6reactions
asayyed910commented, Dec 5, 2017

I have fixed it now it will show only matching url as active rest as inactive

add active class and stay opened when selected
        var url = window.location;
        // for sidebar menu entirely but not cover treeview
        $('ul.sidebar-menu a').filter(function() {
            return this.href != url;
        }).parent().removeClass('active');

        // for sidebar menu entirely but not cover treeview
        $('ul.sidebar-menu a').filter(function() {
            return this.href == url;
        }).parent().addClass('active');

        // for treeview
        $('ul.treeview-menu a').filter(function() {
            return this.href == url;
        }).parentsUntil(".sidebar-menu > .treeview-menu").addClass('active');
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make only the valid active page of a multilevel ...
This is purely a CSS question, and has nothing to do with WordPress, but the answer is simple enough. You need to use...
Read more >
Multilevel Menu Design Best Practices
Apply these guidelines to design intuitive multilevel menus that increase ... If the current page is in a submenu, highlight the submenu link...
Read more >
Bootstrap nested (multilevel) Dropdown - free examples
Nested dropdowns built with Bootstrap 5. Tutorial & templates for multilevel navbar dropdowns menu, activated on hover or on click & much more....
Read more >
How to create a multilevel dropdown menu in React
Multilevel menus are designed to reveal the deeply nested navigations when we click or hover over the submenu items, as shown in the...
Read more >
Bootstrap 5 navbar with multilevel dropdown menu
This is fully responsive multilevel dropdown navbar code sample. It is also called treeview menu. 1-st level dropdowns are opens by click, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found