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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:38:01+00:00 2026-05-11T00:38:01+00:00

I am currently trying to make a navigation-menu where an active-class is applied to

  • 0

I am currently trying to make a navigation-menu where an active-class is applied to the anchors whose href attributes that match the current URL, so I can style that anchor in a way that makes it stand out from the rest of the menu.

This is my mark-up:

<div id='sidebar'>    <h2>Navigation menu</h2>   <h2 class='subnav'><a href='menu1/menu_item1'>Menu item 1</a></h2>   <h2 class='subnav'><a href='menu1/menu_item2'>Menu item 2</a></h2>   <h2 class='subnav'><a href='menu1/menu_item3'>Menu item 3</a></h2>   <h2 class='subnav'><a href='menu1/menu_item4'>Menu item 4</a></h2>   <h2 class='subnav'><a href='menu1/menu_item5'>Menu item 5</a></h2>  </div>  

This is the jQuery:

   jQuery(function($) {        // get the current url       var path = location.pathname.substring(1);         // defining the top subnav anchor       var $top_item = $('#sidebar h2:nth-child(2) a');         // defining all subnav anchors       var $all_items = $('#sidebar h2.subnav a');         // defining the anchors with a href that matches the current url       var $selected_item = $('#sidebar h2 a[@href$='' + path + '']');         // setting the selected menu item'class as active       $selected_item.addClass('active');         // THIS IS WHERE I THINK THE ERROR IS       // if none of the h2.subnav's has a url that matches        // the current location then assume that it's the top one that's active:       if ($all_items('href') !== path) $top_item.addClass('active');    }); 

I am applying the active-class with jQuery, it works fine as long as there is a match between an anchors href and the location url. If the url don’t match any of the anchors I want the active-class to be applied to the $top_item. That part of my jQuery doesn’t work.

I can’t see what the error is, but then again I’m somewhat of a Javascript/jQuery n00b. Any help would be appreciated.

  • 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. 2026-05-11T00:38:02+00:00Added an answer on May 11, 2026 at 12:38 am

    This should do want you want: mark the matching link, and failing that, mark your default one.

    function markActiveLink() {      //Look through all the links in the sidebar    $('div#sidebar a').filter(function() {        //Take the current URL and split it into chunks at each slash       var currentURL = window.location.toString().split('/');        //return true if the bit after the last slash is the current page name       return $(this).attr('href') == currentURL[currentURL.length-1];      //when the filter function is done, you're left with the links that match.     }).addClass('active');     //Afterwards, look back through the links. If none of them were marked,    //mark your default one.    if($('div#sidebar a').hasClass('active') == false) {       $('div#sidebar h2:nth-child(2) a').addClass('active');     }  }  markActiveLink(); 

    Also, I found an official tutorial on this on the jQuery Docs site – scroll to the bottom to see the jQuery code. It’s tighter than mine, although it’s not tailored to your situation.

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

Sidebar

Related Questions

I am currently trying to make sure that a member function of one class
I'm currently trying to make a small application that performs different duties. Right now
Recently I was trying to make a calendar application that will display the current
I'm trying to make a navigation dropdown with jQuery that looks like this: <ul
I'm trying to implement a simple horizontal navigation menu that just shows a single
I am trying to make a navigation that uses simple unordered lists to list
So I am currently trying to make the game Asteroids for a class. The
I'm currently trying to edit a project, that already uses OpenCL.dll, to make it
overflowians, I am trying to represent a 3 tiered Navigation menu that are inside
I am currently trying to make a very simple C program for school that

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.