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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:48:26+00:00 2026-05-26T12:48:26+00:00

I have a menu that links to content on the same page and through

  • 0

I have a menu that links to content on the same page and through jQuery fades div’s in and out, working perfectly.

The first link in the navigation has a class attached to it (current_page_item) and I would like, on click of any other link in the navigation, to remove that class and add it to the clicked link.

So far I’ve tried something like:

$(function() {
    $("#nav-links a").click(function(){
        $("#nav-links ul li").removeClass("current_page_item").find("li:last a").addClass("current_page_item");
        });
    });

which doesn’t work.

I’m very new to javascript so any help would be appreciated.

Thanks!

EDIT:

Here is the HTML for the navigation:

            <div id="nav-wrap">

                <ul class="group" id="nav-links">
                    <li class="current_page_item"><a href="#recent" class="linkclass recent">RECENT</a></li>
                    <li><a href="#work" class="linkclass work">WORK</a></li>
                    <li><a href="#who" class="linkclass who">WHO</a></li>
                    <li><a href="#services" class="linkclass services">SERVICES</a></li>
                    <li><a href="#contact" class="linkclass contact">CONTACT</a></li>
                </ul>

            </div>

EDIT 2:

Using the answers below it still doesn’t seem to achieve what I want. Perhaps it’s because of the jQuery plugin I’m using (magic line). Link

So basically I’d like the ‘line’ to stay underneath what is clicked. Which works if I load a new page with the ‘current_page_item’ in the right place, but as I’m fading DIVs in and out, I can’t do that.

The line bounces back to the li item with ‘current_page_item’ class. Perhaps this code can be modified to add the class to the clicked element?

Here’s the code.

    $(function() {

        var $el, leftPos, newWidth,
            $mainNav = $("#nav-links");

        $mainNav.append("<li id='magic-line'></li>");
        var $magicLine = $("#magic-line");

        $magicLine
            .width($(".current_page_item").width())
            .css("left", $(".current_page_item a").position().left)
            .data("origLeft", $magicLine.position().left)
            .data("origWidth", $magicLine.width());

        $("#nav-links li a").hover(function() {
            $el = $(this);
            leftPos = $el.position().left;
            newWidth = $el.parent().width();
            $magicLine.stop().animate({
                left: leftPos,
                width: newWidth
            });
        }, function() {
            $magicLine.stop().animate({
                left: $magicLine.data("origLeft"),
                width: $magicLine.data("origWidth")
            });
        });
    });
  • 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-26T12:48:27+00:00Added an answer on May 26, 2026 at 12:48 pm

    You can reach clicked element with $(this) and access to it’s parent li element with parent() to add current_page_item class. Below is the final code and you can find a working example here.

    $(function() {
        var $el, leftPos, newWidth,
            $mainNav = $("#nav-links");
    
        $mainNav.append("<li id='magic-line'></li>");
        var $magicLine = $("#magic-line");
    
        $magicLine
            .width($(".current_page_item").width())
            .css("left", $(".current_page_item a").position().left)
            .data("origLeft", $magicLine.position().left)
            .data("origWidth", $magicLine.width());
    
        $("#nav-links li:not('#magic-line') a").hover(function() {
            $el = $(this).parent();
            leftPos = $el.position().left;
            newWidth = $el.width();
            $magicLine.stop().animate({
                left: leftPos,
                width: newWidth
            });
        },function() {
            $magicLine.stop().animate({
                left: $magicLine.data("origLeft"),
                width: $magicLine.data("origWidth")
            });
        }).click(function() {
             $mainNav.find('li').removeClass('current_page_item');
             $(this).parent().addClass('current_page_item');
             $magicLine
                .data("origLeft", $magicLine.position().left)
                .data("origWidth", $magicLine.width());
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this menu structure that is used to navigate through content slider panels.
I have a page with a menu that uses JQuery AJAX calls to populate
I have a menu div that I want to slide down so it's always
I have a jquery menu that has so many entries that it extends further
On my page I have one navigation menu and two content containers. The content
I want to have a menu that when I click replaces the content of
I have a fairly basic dialog maker for jquery that works in 2 out
I have a menu with a (links) tags. In Jquery I'm handling click event
I have a page which loads its inner content using jquery and a simple
The Drupal 6 menu module hides links to content that the current user does

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.