Cool jQuery Predicate Selectors

Posted October 19, 2007 at 5:21 PM by Ben Nadel

Tags: Javascript / DHTML

I was reading up on jQuery last night and came across some cool jQuery predicate filtering in the selector statements. We have all seen the predicate for full attribute value matches:

  • a[ @rel = 'nofollow' ]

This would select all the links in the document object model that have a REL attribute value of "nofollow". In addition to the "=" comparison operator, jQuery provides some additional regular-expression-esque comparison operators.

Start With: ^=

The ^= operator will filter elements whose attribute starts with the given value.

Ends With: $=

The $= operator will filter elements whose attribute ends with the given value.

Contains: *=

The *= operator will filter elements whose attribute contains the given value.

To test this, I whipped up a little demo page. On this demo page we have a number of list items, each of which have an ID that starts with "list" and ends with the one-based index of the list item. Then, we use the jQuery selectors from above to output the text of the matched elements:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>jQuery Attribute Predicate Demo</title>
  • <script type="text/javascript" src="jquery.pack.js"></script>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery Attribute Predicate Demo
  • </h1>
  •  
  • <ul>
  • <li id="list1">One</li>
  • <li id="list2">Two</li>
  • <li id="list3">Three</li>
  • </ul>
  •  
  •  
  • <!---
  • Below are the test that we will run against
  • the unordered list above.
  • --->
  •  
  •  
  • <h2>
  • Tests / Results
  • </h2>
  •  
  • <h3>
  • Full Match: "li[ @id = 'list1' ]"
  • </h3>
  •  
  • <p>
  • <script type="text/javascript">
  • document.write(
  • $( "li[ @id = 'list1' ]" ).text()
  • );
  • </script>
  • </p>
  •  
  •  
  • <h3>
  • Start With Match: "li[ @id ^= 'list' ]"
  • </h3>
  •  
  • <p>
  • <script type="text/javascript">
  • document.write(
  • $( "li[ @id ^= 'list' ]" ).text()
  • );
  • </script>
  • </p>
  •  
  •  
  • <h3>
  • Ends With Match: "li[ @id $= '3' ]"
  • </h3>
  •  
  • <p>
  • <script type="text/javascript">
  • document.write(
  • $( "li[ @id $= '3' ]" ).text()
  • );
  • </script>
  • </p>
  •  
  •  
  • <h3>
  • Contains Match: "li[ @id *= 'st' ]"
  • </h3>
  •  
  • <p>
  • <script type="text/javascript">
  • document.write(
  • $( "li[ @id *= 'st' ]" ).text()
  • );
  • </script>
  • </p>
  •  
  • </body>
  • </html>

Running the above page, we get the following output:

Full Match: "li[ @id = 'list1' ]"

One

Start With Match: "li[ @id ^= 'list' ]"

OneTwoThree

Ends With Match: "li[ @id $= '3' ]"

Three

Contains Match: "li[ @id *= 'st' ]"

OneTwoThree

The more I read up on jQuery, the more I realize how sweet-ass it is. jQuery is the "Easy" button of the user interface world.


 
 
 

 
jQuery: The Easy Button For User Interface Development  
 
 
 


Reader Comments

Oct 20, 2007 at 12:08 PM // reply »
1 Comments

>> jQuery is the "Easy" button of the user interface world.

Exactly!

Thanks for this nice explanation of the various specific selectors.


Mar 16, 2009 at 9:57 AM // reply »
1 Comments

Quite interesting.
Note however that the code above will not work with jQuery v 1.3 and above.

You simply need to remove the "@".

More info at :
http://docs.jquery.com/Release:jQuery_1.3#Upgrading


Mar 16, 2009 at 10:03 AM // reply »
10,638 Comments

@Cypher,

That is correct. This XPath-like attribute selection was used, then deprecated, and now, in the latest releases, completely removed.


Vx
Apr 1, 2009 at 12:38 AM // reply »
4 Comments

so how do you do this in the new jQuery?


Vx
Apr 1, 2009 at 12:39 AM // reply »
4 Comments

never mind... see the comment by Ben, i can't find the api reference though? any link for that?


Apr 1, 2009 at 9:30 AM // reply »
10,638 Comments

@Vx,

Check out http://docs.jquery.com.


Vx
Apr 2, 2009 at 1:48 AM // reply »
4 Comments

:) I've been using that and a search through there doesn't show any results for Predicates, or *=
I haven't found it in the docs, could you?


Apr 2, 2009 at 8:31 AM // reply »
10,638 Comments

@Vx,

Check out Attribute Filters in the Selectors section:

http://docs.jquery.com/Selectors

Basically, in newer version of jQuery, you just have to remove teh @ sign.


Vx
Apr 3, 2009 at 12:14 AM // reply »
4 Comments

Ahh I see it now... Thanks :)
I've been through the selectors page a dozen times, i wonder how i missed it! hehe.
$("input[id*='wait'][hidden]").val() would work now...
Thanks


Apr 3, 2009 at 8:07 AM // reply »
10,638 Comments

@Vx,

No problem man, glad you got it working.


Nov 19, 2009 at 6:21 PM // reply »
1 Comments

For anyone who's interested I've published V2 of a jQuery reference if you want to have a look at that it's here: http://www.skidoosh.co.uk/jquery/jquery-selectors-and-attribute-selectors-reference-and-examples-v2/ I'll be updating it for 1.3 in a few days.


Aug 2, 2011 at 3:56 PM // reply »
1 Comments

one thing to be careful of is jQ does not like spaces in this selector method (found the hard way :D ).


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 3, 2012 at 10:49 PM
How I Got Node.js Running On A Linux Micro Instance Using Amazon EC2
Wow this was really helpful! Only thing I would add is you need to update your .bash_profile after you edit the secure_path. This is what I did: $ . ~/.bash_profile Otherwise, NPM won't be found. ... read »
Feb 3, 2012 at 10:14 PM
Pushing Base64-Encoded Images Over HTML5 WebSockets With Pusher And ColdFusion
@Ben, Just wanted to let you know that pusher are soon to start limiting sizes on messages. This was the detail that came through in the Feb dispatch: "However, we will soon be limiting the s ... read »
Feb 3, 2012 at 5:05 PM
Regular Expressions Make CSV Parsing In ColdFusion So Much Easier (And Faster)
I tried using your RegEx in my C# program, but it was matching an extra empty-string at the end and so I would end up with an extra field that doesn't exist, so I changed it to this: (^|,)("(?: ... read »
Feb 3, 2012 at 3:47 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
Josh Cyr posted this on Twitter just a little bit ago. Thought it was appropriate. http://stackoverflow.com/questions/1619152/how-to-create-rest-urls-without-verbs/1619677#1619677 ... read »
Feb 3, 2012 at 2:28 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
@Michael, You definitely make a good point (and extra points for quoting movies - I love movies). When you use a return() statement to define the object's public API, it does provide a consistent a ... read »
Feb 3, 2012 at 2:04 PM
Changing The Execution Context Of Your Self-Executing Function Blocks In JavaScript
To quote Jurassic Park: "Just because you can doesn't mean you should". I completely, utterly disagree with the thought that this is more readable. Consider the current module pattern: if ... read »
Feb 3, 2012 at 1:10 PM
REST API Design Rulebook By Mark Masse
@Jordan, Yeah, WRML was created by Mark Masse (author of the book). I also found it to be a bit convoluted. I suppose it is intended to allow the Client to be able to programmaticaly respond to cha ... read »
Feb 3, 2012 at 1:08 PM
ColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)
@Jason, To be honest, I don't have good answers for that kinds of stuff. And, to the point, that is specifically why I *really* liked the REST API Design Rulebook by Mark Masse - he just cuts throu ... read »