1

I've the following mark-up. I want to match the anchor tag based on the part of the URL.

Actually I've markup as below. And i know the sorting field name. I want to select the anchor based on the field name eg. sort:resource_locator based on this I want to select the first anchor tag and I want to give the class as per the direction such that it will add an small up/down arrow to it.

<th><a href="/sme_leads/index/page:1/sort:resource_locator/direction:asc">Resource Locator</a></th>
<th><a href="/sme_leads/index/page:1/sort:business_name/direction:asc">Business Name</a></th>

I want to know how can I select the anchor tag based on the sort:resource_locator information.

I know $ and ^ for end and start respectively.

3
  • you want to add the class/arrow when the user clicks on the anchor? or just on document load? Commented Sep 5, 2012 at 17:14
  • does this need to be dynamic, or ONLY for sort:resource_locator Commented Sep 5, 2012 at 17:17
  • I know this from server side and I can pass it to javascript. It can be any field in my table head. Commented Sep 5, 2012 at 17:22

2 Answers 2

3

You could use the Attribute Contains Selector [name*="value"]:

$('a[href*="sort:resource_locator"]')

DEMO.

Sign up to request clarification or add additional context in comments.

Comments

2

To select those a elements with a sort: in their href:

$('a').filter(
    function(){
        return this.href.indexOf('sort:') !== -1;
    });

To assign a class based on the string following the direction: string (asc or desc):

$('a').filter(
    function(){
        return this.href.indexOf('sort:') !== -1;
    }).addClass(
        function(){
            var href = this.href,
                matches = href.match(/direction\:([a-z]+)/);
            return matches[1];
        });​

JS Fiddle demo.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.