Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:36:33+00:00 2026-05-25T11:36:33+00:00

I have a menu which is stylized through jquery and CSS so the structure

  • 0

I have a menu which is stylized through jquery and CSS so the structure is like that:

<div id="nav_top" class="clearfix round_top">
    <ul class="clearfix round_all">
        <li class="current"><a href="index.php"><img src="images/icons/home.png"/>Home</a></li>

        <li><a href="#"><img src="images/icons/users_2.png"/>Users</a>
            <ul class="dropdown">
                <li><a href="users.php">Create user</a></li>
                <li><a href="view_users.php">View users</a></li>
                <li><a href="#" class="has_slide">Group management</a>
                    <ul class="slideout">   
                        <li><a href="#">View groups</a></li>
                        <li><a href="#">Manage permissions</a></li>
                    </ul>
                </li>
                <li><a href="#">Another menu item</a></li>
            </ul>
        </li>

    </ul>
</div>

So, as you can see, many levels could have an anchor.

The main idea is to have the TOP level with current because that’s the only part you can see without hovering.

My first approach was to capture the current URL throuh window.location.pathname and then iterate through every $('#nav_top a') with a function like this one:

$('#nav_top a').each(function(){
if ( path.search($(this).attr('href') != -1){
   $(this).parent().parent().parent().addClass('current');
   $('.current').removeClass('current');
}
});

I also know I could select by href but I was going to make a performance test between both methods but, anyway, the point is that this works only for one of the levels because of the .parent() nesting.

How could I make this extensible to all levels?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:36:33+00:00Added an answer on May 25, 2026 at 11:36 am

    You can try this out. Using the jQuery closest() http://api.jquery.com/closest/.

    Notice that I also switched around removing and adding the class. The way you had it would just remove it from the element you just added it to.

    $('#nav_top a').each(function(){
       if ( path.search($(this).attr('href') != -1){
          $('.current').removeClass('current');
          $(this).closest('.top-level').addClass('current');
       }
    });
    

    Edit:

    Add a class of top-level to your top level <li>s

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Have a menu which look like this <div id=products_menu class=span-9 last> <ul> <li><a
I have a database menu structure which I would like to add to the
I have a menu div which has a dark background. Inside it, I have
I have a web part which uses many SharePoint controls like menu, SPGrid, tool
I have a Drupal Menu which I created through the interface. I wish to
I have footer menu in drupal which contains so many menu items. <ul class=menu>
If you have a layout that has in a menu which gets its menu
I have a navigation menu which looks like this: I have to split it
I have a CSS hover menu which works in all browsers except... surprise --
have a menu item which displays a modal div. wish the created div to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.