jQuery Custom Selectors - Holy Cow That Is So Badass!

Posted February 23, 2007 at 4:49 PM

Tags: Javascript / DHTML

One of the things that is so cool about jQuery is the user of selectors to match DOM elements. Glen Lipka just pointed out to me that you can create your own custom selectors. Holy cow! That is so bad ass, you have no idea. Since I am very new to all of this, I figure I would write a little experiment to see how it worked (NOTE: I am new, so take this with a grain of salt).

I am going to create a list of links that have a REL attribute and a HOTNESS attribute. Then I am going to create my own selector "hottie" which selects only links where the REL attribute is "girl" and the HOTNESS attribute is greater than or equal to 9.

 Launch code in new window » Download code as text file »

  • <html>
  • <head>
  • <title>jQuery Custom Selector Test</title>
  • <script type="text/javascript" src="jquery-latest.pack.js"></script>
  • <script type="text/javascript">
  •  
  • // Extend the jQuery object to include the
  • // custom selector "hottie" for the ":" expression.
  • jQuery.extend(
  • jQuery.expr[ ":" ],
  • {
  • hottie : (
  • "jQuery( a ).attr( 'rel' ) == 'girl' && " +
  • "jQuery( a ).attr( 'hotness' ) >= 9"
  • )
  • }
  • );
  •  
  •  
  • // This will highlight the girls who are hotties.
  • function FindHotties(){
  •  
  • $( "ol a:hottie" ).css( "font-weight", "bold" );
  •  
  • }
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <ol>
  • <li><a rel="girl" hotness="9.0">Sarah</a></li>
  • <li><a rel="girl" hotness="8.0">Libby</a></li>
  • <li><a rel="girl" hotness="9.0">Azure</a></li>
  • <li><a rel="girl" hotness="8.5">Cindy</a></li>
  • </ol>
  •  
  • <p>
  • <a href="javascript:void(0);"
  • onclick="FindHotties();"
  • >Find Hotties</a>
  • </p>
  •  
  • </body>
  • </html>

Running this page, the initial output is:

  1. Sarah
  2. Libby
  3. Azure
  4. Cindy

But, once I click on the "Find Hotties" link, the output gets changed to:

  1. Sarah
  2. Libby
  3. Azure
  4. Cindy

HOW FREAKIN' COOL IS THAT?!?!? jQuery is becoming more and more exciting by the day. I can imagine using stuff like this all over the place. My gut feeling says that this sort of thing is not the most "performant", but JavaScript is so fast on new computers, what can it matter. Plus, it's almost too easy to use, I can't pass it up.

Here is a huge list of custom selectors that Glen gave me. Are you beginning to see the possibilities?

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Feb 23, 2007 at 6:32 PM // reply »
28 Comments

Sweet man! You're really digging jQuery from what I see! :)


Jordan Clark
Feb 23, 2007 at 6:54 PM // reply »
2 Comments

Hey Ben, cool for showing how easy it is to make a custom selector, but shouldn't you use a namespace to add custom/arbitrary attributes to existing tags? Otherwise your code wont validate as xhtml.


Feb 24, 2007 at 6:48 AM // reply »
10 Comments

And how do you declare namespaces Jordan?


Feb 24, 2007 at 10:49 AM // reply »
5,406 Comments

@Rey,

This stuff is just too cool! How could you not like it. Do realize how easy this sort of thing will make readable selection code? Too easy.

@Jordan,

Good point, I am not one who often adds custom attributes so it didn't occur to me. This jQuery stuff is new, so I am only beginning to see the possibilities.

@Will,

While I cannot answer with 100% accuracy, you would create a namespace by declaring it in the HTML tag. I don't know the exact syntax, but I think, if I wanted to make a "Kinky" name space for Kinky Solutions, I would use this HTML tag:

<html xmlns:kinky="www.kinkysolutions.com">

Then, in the code, since "hottie" is a custom attribute, I should define it as such:

[a kinky:hottie="9.5"]...[/a]

This will prevent attribute clashes if XHTML 2.0 decides to add the "hottie" attribute to the link tag.

Of course, I have never done this, so I may be off, but I think that is the gist of it.


Feb 25, 2007 at 6:38 PM // reply »
37 Comments

You could also use the REL tag as a XHTML compatible attribute. But as a general rule, I have trouble understanding why XHTML is so limited on this score. Wy can't we just add attributes? What is the downside? Will a browser not work with it?

I just don't see why it matters for 99.9999% of all projects.

Glen


stylo
Feb 27, 2007 at 5:24 AM // reply »
6 Comments

--Otherwise your code wont validate as xhtml.

But who cares what a validator somewhere thinks? It affects nothing.


Feb 27, 2007 at 7:26 AM // reply »
5,406 Comments

Stylo,

While I would agree with you in theory, in practice, a surprising number of clients will require that their site validates as XHTML. But other than client needs, agreed, it affects nothing.


insic
May 28, 2009 at 7:55 AM // reply »
1 Comments

This is cool, Thanks.


Post Comment  |  Ask Ben

Recent Blog Comments
Jul 4, 2009 at 9:42 AM
FLV 404 Error On Windows 2003 Server
I bookmarked this page. Thanks for given this great post.... ... read »
Jul 4, 2009 at 4:00 AM
Terms Of Service / Privacy Policy Document Generator
thanks ben, I'm not a big fan of contracts so to find your no no-nesense ToS generator has helped me no end. all the best matt ... read »
Justice
Jul 3, 2009 at 11:10 PM
Create A Running Average Without Storing Individual Values
@Ben, I think you're going about this the wrong way. You're trying to use complicated techniques when there is a simple and beautiful technique readily available (a la Gary Funk's comment). Instead ... read »
Bob
Jul 3, 2009 at 9:19 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
a good technical explanation http://crossfitphoenix.typepad.com/crossfit_phoenix_forging_/the-overhead-squat.html ... read »
Jul 3, 2009 at 9:03 PM
Create A Running Average Without Storing Individual Values
If I wanted to do this and only carry two numbers, I'd keep track of the sum and N. Then you are pretty much accurate all the time. average = (sum + new_number) / (N + 1) But all this was in a for ... read »
Roland Collins
Jul 3, 2009 at 8:58 PM
Create A Running Average Without Storing Individual Values
@Martin - not just floating point though. Depending on what langauge you're working in, decimals can cause just as many headaches if they're not precise enough. But again, for most applications, th ... read »
Isnogood
Jul 3, 2009 at 7:16 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
Watch this http://www.nsca-lift.org/videos/default.shtml ... read »
Aaron
Jul 3, 2009 at 7:13 PM
Project HUGE: Get Big, Phase One (Chat Waterbury - Huge In A Hurry)
I've just finished the 3rd week of phase 3, and have to agree that the overhead squats are hard. I think this is most due to the wide grip on which places more pressure on your upper back. Only this ... read »