Blog


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

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

To those of you who are familiar with jQuery we've had a similar selector, :has(), for quite some time:

$("a:has(img)")

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;

Then the following will work as you would expect it to:

$("a < img")

It's really nice having an extensible selector engine at your disposal.

Tags: selectors, javascript, jquery

Current Projects

jQuery JavaScript Library

jQuery

Comprehensive DOM, Event, Animation, and Ajax JavaScript Library.

Recent Projects

Pro JavaScript Techniques

JavaScript Book

The best techniques for professional JavaScript. Published by Apress.