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

  • Home
  • SEARCH
  • 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 8782545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:35:25+00:00 2026-06-13T20:35:25+00:00

Selector ‘ Attribute Starts With’ selects elements that have the specified attribute with a

  • 0

Selector ‘Attribute Starts With' selects elements that have the specified attribute with a value beginning exactly with a given string, according to the documentation. I would like to to do the exact opposite thing – I would like to select those elements whose value of a specified attribute is at the beginning of given string.

Consider following example:

<a href="/about-us/">About us</a>
<a href="/products/">Products</a>
<a href="/contact/">Contact</a>

Current URL is /products/some-catogory/sub-category/product/?x=y&z=x and I want to highlight the second menu item. What’s the best way to do it?

  • 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-13T20:35:27+00:00Added an answer on June 13, 2026 at 8:35 pm

    I’d suggest:

    var path = document.location.pathname;
    $('a').filter(
        function(i,e){
            return $(this).attr('href').indexOf(path) == 0;
        }).addClass('highlight');
    

    JS Fiddle demo.

    Where the highlight class-name defines, appropriately enough, the highlighting.

    This is tested with the following HTML:

    <a href="/about-us/">About us</a>
    <a href="/products/">Products</a>
    <a href="/_display/">_display</a>
    <a href="/contact/">Contact</a>​
    

    Bear in mind this is proofed on JS Fiddle, so it’ll match the the third a element (since that’s the one that matches.

    Edited to offer a slightly more generic solution, in which you can match by a given directroy/subdirectory:

    function directory(path) {
        if (!path) {
            return false;
        }
        else {
            var _tmp = path.split('/'),
                directories = [];
            for (var i = 0, len = _tmp.length; i < len; i++) {
                if (_tmp[i].length > 0 && _tmp[i].indexOf('?') == -1) {
                    directories.push(_tmp[i]);
                }
            }
            return directories;
        }
    }
    // in the real world use:
    // var path = document.location.pathname,
    var path = '/products/some-catogory/sub-category/product/?x=y&z=x',
        dir = directory('/products/some-catogory/sub-category/product/?x=y&z=x')[0];
    $('a').filter(
        function(i, e) {
            return $(this).attr('href').indexOf(dir) == 0 || $(this).attr('href').indexOf('/' + dir) == 0
        }).addClass('highlight');​
    

    JS Fiddle demo.

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

Sidebar

Related Questions

Possible Duplicate: CSS Selector that applies to elements with two classes So I have
the empty selector says that: Matches all elements that have no children (including text
So I have a selector that need to be perform after certain amount of
I have a calender selector that looks like <asp:ImageButton ID=calStartImage runat=server ImageUrl=../images/SmallCalendar.gif AlternateText=Please select
Is there any selector that do this? Select all input field which value is
I have an image selector that allows to choose an image from a gallery,
I have a jquery selector that looks like: var val = John's cars; $('#subject
I have a selector that works like this: $('#container textarea, #container :text'); However this
Possible Duplicate: jQuery attribute selector for multiple values I have this $('input:radio[name=foo]').change(function() { blah();
Possible Duplicate: CSS Selector that applies to elements with two classes I've got 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.