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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:12:03+00:00 2026-05-25T20:12:03+00:00

I made an active state for my menu on a certain urls. I have

  • 0

I made an active state for my menu on a certain urls. I have urls like this:

  • /products/other-clothing/sporting/adults-anzac-australia-polo
  • /products/other-clothing/sporting/adults-nz-tee
  • /products/bags/backpacks

My code gets the folder from after the / so other-clothing, sporting, etc.
It is working fine, I just assume there is a more efficient way to write the code.

Here is my code:

jQuery(".product-nav li a").each(function() {
  // URL url
  var cat = location.pathname.split("/")[2];
  var subcat = location.pathname.split("/")[3];
  var c = "/products/" + cat + "/" + subcat;

  // A tag url
  var acat = this.href.split("/")[4];
  var asubcat = this.href.split("/")[5];
  var e = "/products/" + acat + "/" + asubcat;

  if(e == c) {
    jQuery(this).parent().addClass("active");
    jQuery(this).parent().parent().parent().addClass("active");
  }
});

If anyone can provide a cleaner way of writing the code that’d be great. I probably dont need "/products/" +.

  • 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-25T20:12:03+00:00Added an answer on May 25, 2026 at 8:12 pm

    Here’s a brief whack at it:

    jQuery(".product-nav li a").each(function() {
      // URL url
      var c = location.pathname.split('/').slice(2, 4)
      // A tag url
        , e = this.href.split('/').slice(4, 6)
      ;
    
      if(e[0] == c[0] && e[1] == c[1]) {
        jQuery(this).parentsUntil(
          'div:not(.subnav)', // go up the tree until the 1st div that isn't .subnav
          '.product-nav li, .subnav' // and only match these parents
        ).addClass('active');
      }
    });
    

    .parent().parent().parent()... has a pretty bad code smell to it but can’t be improved without a look at your markup. You should probably be using .closest() instead.

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

Sidebar

Related Questions

I have made this centered navigation bar, and I wanted to add a :active
I've made a one page layout website, where the menu doesn't show the active/current
I have made an App for watching Active tcp/udp connections for the local pc.
I just wonder if anyone knows or made a wrapper around Active Directory to
I have written an ActiveX control in C# and have made it working using
Made this nice little loop for hiding and showing div's, works as a charm
I've made a two level menu using CSS and HTML (ul's etc). What I
I have been struggling with this problem for a few days now and I
I have a navigation menu and when 'contact is clicked' a div opens at
made a menu out of the following code but the original href in the

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.