We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Add class to active link that has children

wildeeep asked
Medium Priority
Last Modified: 2012-05-06
I am using the superfish menu system on a site for a navigation system the html markup is as follows

<ul class="sf-menu sf-vertical">
      <li><a href="index.html">Home</a></li>
      <li><a href="about.html">About Us</a>
                  <li><a href="#">Some Staff Member</a>
                  <li><a href="#">Some Staff Member2</a>
      <li><a href="#">Our Principles</a></li>
      <li><a href="#">Contact</a></li>

I am using jquery to get the current page URL and match it to the link in href and then add the class="active" so i can highlight the current page, this all works fine using

$(".lnav li a").each(function() {
            if(this.href == window.location.href.split("#")[0]) {

by also adding the class="current" to a parent <li> that has a child <ul> ii can keep the child menu open in that section so was wondering if its possible to do his using the same method. I guess i need to add the class="current" to the top parent <li> if any of the child a hrefs match the URL, so in the example above if we were in the page linked from 'Some Staff Member2' i would need <li class="current"><a href="about.html">About Us</a>

Watch Question

Albert Van HalenAnalyst developer

You can use this jquery selector
// Get the topmost li from the current class

Open in new window


Thanks, not quite what i needed though. I need to set the topmost li with the class of current so then the supefish script will keep the menu open.
Albert Van HalenAnalyst developer

Oops, it should be

Open in new window


Thanks this does work but seems to also set the child <li> with the current class too, i only need it for the top parent <li>

Analyst developer
It doesn't add the class to the child li, the rules for that class are inherited from the parent.
If you don't want inheritance, you better apply the class to the a element inside the li.

Try this
$("a", $(".active").parents("li:last")).addClass("current")

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Works perfectly, thanks for a great solution.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.