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 8507905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:00:08+00:00 2026-06-11T03:00:08+00:00

Multiple nav sections all with the same links. The active section is shown in

  • 0

Multiple nav sections all with the same links. The active section is shown in the first nav via an active class (.active). I’d like to highlight the same link in the subsequent nav sections.

Originally, I used a cloning method (credit due to you fine people, jQuery replaceWith() each element with the first) and although it worked perfectly, it interfered with some other functions (because it removes the original elements from the page and replaces them).

So, I need to figure out another, clever way to check the active class of the first section, find the matching links in the subsequent sections, and apply the class.

I’m thinking something like this… but something efficient and less ugly. The answer below does work.

HTML:

<nav class="postNav">
    <ul>
        <li class="active"><a href="#pageHeader">link to header</a></li>
        <li><a href="#one">link to other posts</a></li>
        <li><a href="#two">link to other posts</a></li>
        <li><a href="#three">link to other posts</a></li>
        <li><a href="#four">link to other posts</a></li>
        <li><a href="#five">link to other posts</a></li>
    </ul>
</nav>

JavaScript:

var navs   = $('.postNav'),
    first  = navs.first(),
    active = first.find('.active a').attr('href'),
    anchrs = navs.not(first);

anchrs.find('li').removeClass('active');
anchrs.find('a[href=' + active + ']').parent().addClass('active');

UPDATE


Maybe by seeing which child in the index is active? Then I don’t need to filter by the anchor…

UPDATE + Final Answer


My Selector cached version of jfriend’s answer:

var nav   = $('.postNav'),
first = nav.filter(':eq(0)'),
notF  = nav.filter(':gt(0)'),
index = first.find('.active').index();

notF.each(function(){
    $(this).find('li').removeClass('active').eq(index).addClass('active');
});
  • 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-11T03:00:10+00:00Added an answer on June 11, 2026 at 3:00 am

    You can do it like this using the index position of an li within its ul and jQuery’s .index() and .eq() makes that easier:

    var index = $(".postNav .active").index();
    $(".postNav ul").each(function() {
        $(this).children("li").removeClass("highlight").eq(index)
            .not(".active").addClass("highlight");
    });
    

    Working demo: http://jsfiddle.net/jfriend00/dWaCn/

    This works as follows:

    • Finds the active element and get its index relative to its siblings
    • Find all the postNav ul tags
    • Find all the li children in each one of those
    • Remove any previous “highlight” class
    • Get the matching index sibling
    • Remove the active one so we don’t change it
    • Add the highlight class
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

multiple webapp running on same tomcat using same jvm. sometime, one webapp that have
If multiple events are posted using wxPostEvent/wxCallAfter from same thread e.g. on button click
Given multiple anchor tags: <a class=myclass href=...>My Text</a> How do I select the anchors
I have this nav code in multiple JSP files: <ul id=nav> <li ><a href=/home>Home</a></li>
Can we use multiple tags on the same page in html5? I've read this
I want to add a nav bar to the top of all my pages
/* Multiple Threads Executing * Author Myth17 */ class T1 implements Runnable { public
I have a page with multiple divs that hold different sections of content. Initially,
I have a problem with cycle plugin multiple pager in the same page I
I want to include the same navigation menu on multiple pages, however I do

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.