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

  • Home
  • SEARCH
  • 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 9224639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:22:24+00:00 2026-06-18T04:22:24+00:00

I have an HTML structure like the following: <ul id=mainMenu> <li class=itm_1> Text… </li>

  • 0

I have an HTML structure like the following:

<ul id="mainMenu">
    <li class="itm_1">
        Text...
    </li>
    <li class="itm_2">
        Text...
        <ul>
            <li class="itm_3">
                Text...
                <ul>
                    <li class="itm_4">
                        Text...
                    </li>
                    <li class="itm_5">
                        Text...
                    </li>
                </ul>
            </li>
            <li class="itm_6">
                Text...
            </li>
        </ul>
    </li>
</ul>

What I like to do, is to add a class to the active element and then in any other element in the same path to add another class like the example bellow:

<ul id="mainMenu">
    <li class="itm_1">
        Text...
    </li>
    <li class="itm_2 ANCESTOR_ACTIVE">
        Text...
        <ul>
            <li class="itm_3 ANCESTOR_ACTIVE">
                Text...
                <ul>
                    <li class="itm_4">
                        Text...
                    </li>
                    <li class="itm_5 ACTIVE">
                        Text...
                    </li>
                </ul>
            </li>
            <li class="itm_6">
                Text...
            </li>
        </ul>
    </li>
</ul>

The element that will be considered as active I can find it and add the class of active, but I cannot find the way to add the ancestor_active class in the elements above the active element in the same path.

For now in my script I have the following code:

<?php
    $current_page = $page_id;    // Lets say is 5
?>
<script type="text/javascript">
    var cp = {
        p : <?php echo $current_page; ?>
    };

    jQuery(document).ready(
        function($)
        {
            $('.itm_' + cp.p).addClass('active');
        }
    );
</script>

Note : The nested arrays are not always with the same depth. In some cases there are other more nested lists and in some othere cases there are not sub lists
how can I climbe to the top element #mainMenu and add the classes “ancestor_active” to each li element in the same path using jQuery?

  • 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-06-18T04:22:25+00:00Added an answer on June 18, 2026 at 4:22 am

    Use parents() to look up the tree for all matches

    $('#mainMenu li').click(function(){
      $('#mainMenu li.active').removeClass('active');
       $(this).addClass('active').parents('li').addClass('active');
    });
    

    API reference http://api.jquery.com/parents/

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

Sidebar

Related Questions

I have the following html structure. <td class=coll-1> <b><a href=#>Some link text</a></b> <p>Description lorem
i have this following html structure usilg ul and li. <ul class=treeview id=productTree> <li
I have the following HTML structure <div class=categories> <div class=category>1</div> <div class=category>2</div> <div class=category>3</div>
I have an html form that has the following structure: <input type=text name=title />
I have the following html structure: <div class=container> <div class=header></div> <div class=sidebar></div> <div class=content-loading></div>
I have the following HTML structure <div class=store><a href=#>GERMANY</a></div> <div class=store_details> <p>Stores in Germany</p>
I have the following html structure: <div class=divClass> <a class=aClass href=#> <img src=xxxx.jpg <span
I have the following HTML structure,which is a handlebars.js template.: <div class=row id={{serviceId}}> <div
I have the following html structure <div id=container> <div class=label> @Html.LabelFor(m => m.SomeProperty) </div>
I have the following HTML structure: <div class=list-comments one> <ul> <li> <span>1</span> <p class=title><a

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.