Qualified Selectors in jQuery

Shaun Inman wrote a quick post on what he’s calling “CSS Qualified Selectors“. The syntax that he’s proposing looks something like this:

a < img&#91;/js&#93;

Which says "Find me all <b>a</b> elements that have an <b>img</b> element inside of them."

To those of you who are familiar with jQuery we've had a similar selector, <a href="http://docs.jquery.com/Selectors/has#selector">:has()</a>, for quite some time:


If you’re totally bent on Shaun’s syntax (don’t care for :has, I would assume) here’s the two-line plugin that’ll give it to you in jQuery:

jQuery.parse.push(/^\s*(<)(\s*)(.*)$/); jQuery.expr["<"] = jQuery.expr[":"].has;[/js] Then the following will work as you would expect it to: [js]$("a < img")[/js] It's really nice having an extensible selector engine at your disposal.

Posted: May 5th, 2008

Subscribe for email updates

27 Comments (Show Comments)

Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.

Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

John Resig Twitter Updates

@jeresig / Mastodon

Infrequent, short, updates and links.