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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:10:16+00:00 2026-05-26T00:10:16+00:00

I have the following code which tries to add a class of selected to

  • 0

I have the following code which tries to add a class of selected to a link that matches the url:

var pathname = window.location.pathname;

$(document).ready(function ()
{
    $('ul#ui-ajax-tabs li a').each(function()
    {
        if (pathname.indexOf($(this).attr('href')) == 0)
        {
            $(this).parents('li').addClass('selected');
        }
    });
});

1.) So for example if I have a url like /Organisations/Journal and /Organisations/Journal/Edit a link with Organisations and Journal will show as being selected. This is fine!

2.) However sometimes I have urls like: /Organisations and /Organisations/Archived and if I have a link to the Archived then both will be selected BUT I don’t want this to happen because the Organisations like doesn’t have the second part of the url so shouldn’t match.

Can anyone help with getting this working for the second types Without breaking the first type? Also none of this can be hardcoded regex looking for keywords as the urls have lots of different parameters!

Cheers

EXAMPLES:

If the url is /Organisations/ Or /Organisations then a link with /Organisations should be selected. If the URL is /Organisations/Journal/New then a link with /Organisations/Journal or /Organisations/Journal/New would be selected.

BUT if I have a url with /Organisations/Recent and have two links: /Organisations and /Organisations/Recent then only the second should be selected! So the thing to note here is that it must have a third parameter before it should look for bits of the URL more loosly rather than an exact match.

Remember it might not always be Organisations so it can’t be hardcoded into the JS!

  • 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-26T00:10:16+00:00Added an answer on May 26, 2026 at 12:10 am

    Edit: My previous solution was quite over-complicated. Updated answer and fiddle.

    var pathname = window.location.pathname;
    
    $(document).ready(function ()
    {
        var best_distance = 999; // var to hold best distance so far
        var best_match = false;  // var to hold best match object
    
        $('ul#ui-ajax-tabs li a').each(function()
        {
            if (pathname.indexOf($(this).attr('href')) == 0)
            {
                // we know that the link is part of our path name.
                // the next line calculates how many characters the path name is longer than our link
                overlap_penalty = pathname.replace($(this).attr('href'), '').length;
                if (overlap_penalty < best_distance) { // if we found a link that has less difference in length that all previous ones ...
                    best_distance = overlap_penalty; // remember the length difference
                    best_match = this; // remember the link
                }
            }
        });
    
        if (best_match !== false)
        {
            $(best_match).closest('li').addClass('selected');
        }
    });
    

    The best match is calculated like so:

    Assume our pathname is “/foo/bar/baz/x”.
    We have two links in question:

    • /foo/bar/
    • /foo/bar/baz

    This is what happens:

    1. /foo/bar/baz/x (the first link’s url is deleted from the path name pathname.replace($(this).attr('href'), ''))
    2. “baz/x” is what remains.
    3. The character count (length) of this remainder is 5. this is our “penalty” for the link.
    4. We compare 5 to our previous best distance (if (overlap_penalty < best_distance)). 5 is lower (=better) than 999.
    5. We save this (being the <a href="/foo/bar/"> DOM object) for possible later use.
    6. The second link is handled in the same manner:
    7. /foo/bar/baz/x (the second link’s url is deleted from the path name)
    8. “/x” is what remains.
    9. The character count of this remainder is 2.
    10. We compare 2 to our previous best distance (being 5). 2 is lower than 5.
    11. We save this (being the <a href="/foo/bar/baz"> DOM object) for possible later use.

    In the end, we just check if we found any matching link and, if so, apply addClass('selected') to its closest <li> parent.

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

Sidebar

Related Questions

i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which I want to use to make sure that
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
I have the following code which is fine if I give invalid parameters (though,
I have the following code which will generate two pdf files containing plots to
I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();

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.