Add class to active link that has children

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>

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Albert Van HalenAnalyst developerCommented:
You can use this jquery selector
// Get the topmost li from the current class

Open in new window

wildeeepAuthor Commented:
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 developerCommented:
Oops, it should be

Open in new window

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

wildeeepAuthor Commented:
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>

Albert Van HalenAnalyst developerCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wildeeepAuthor Commented:
Works perfectly, thanks for a great solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.