Skip to content

Instantly share code, notes, and snippets.

@padolsey
Created April 27, 2010 08:07
Show Gist options
  • Select an option

  • Save padolsey/380481 to your computer and use it in GitHub Desktop.

Select an option

Save padolsey/380481 to your computer and use it in GitHub Desktop.
// Idea from http://dbaron.org/log/20100424-any
jQuery.expr[':'].any = function(el, i, match) {
return jQuery.find.matches(match[3], [el]).length > 0;
};
jQuery('body :any(div, form) p'); // Same as jQuery('body div p, body form p')
jQuery('div:any(.foo,.bar)'); // Same as jQuery('div.foo, div.bar')
@Mottie

Mottie commented Aug 9, 2010

Copy link
Copy Markdown

I was thinking that it might be nice to add this to the core, but maybe drop the :any so we can use selectors like:

jQuery('body (div, form) p');

@aroder

aroder commented Apr 20, 2011

Copy link
Copy Markdown

I like it, @Mottie. I prefer

jquery('body [div, form] p');

as seen in regexp

@beverloo

Copy link
Copy Markdown

Both your suggestions would mean that jQuery's selector syntax would differ from the actual CSS syntax. It'd be best for the two to keep in sync as much as possible.

One thing you should keep in mind is that the specification currently mentions :matches instead of :any. However, as there are two independent implementations, it's likely for the specification to be updated in order to reflect that.
http://dev.w3.org/csswg/selectors4/Overview.html#matches

@aroder

aroder commented Apr 21, 2011

Copy link
Copy Markdown

Good point, @beverloo. The jQuery selector syntax has helped me better understand CSS. Syntactic sugar isn't enough of a benefit to justify moving away from the standard.

@mathiasbynens

Copy link
Copy Markdown

A different approach — http://twitter.com/padolsey/status/119790781417537536:

$.expr[':'].matches=(function(_){return function(el,i,m){return!!_(m[3]||'*',[el])[0];};}($.find.matches)); // CSS4 :matches selector for jQ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment