tags:

views:

52

answers:

2

Hi

I was just looking at the jquery ui button plugin and noticed this

$("button, input:submit, a", ".demo").button();

I never seen something like this. Is this like multiple selects in one jquery selector?

A: 

yes, perfectly legal and VERY useful.

Tom Cabanski
When did they come out with that? I don't even see it in the documentation(might be looking in the wrong area)
chobo2
@chobo it's been there since version 1.0 try looking at it here http://api.jquery.com/jquery/
Reigel
I must be blind I don't see any examples of it.
chobo2
+3  A: 

The second argument (".demo" in your example) is the context, basically your selector is restricted to match only descendants of a determined context:

$(expr, context)

Is just equivalent to use the find method:

$(context).find(expr)

Give a look to the documentation of the jQuery function:

Selector Context

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:

$('div.foo').click(function() {
  $('span', this).addClass('bar');
  // it will find span elements that are
  // descendants of the clicked element (this)
});

Also notice that the selector you post "button, input:submit, a", is called Multiple Selector, and there you can specify any number of selectors to combine into a single result, just by separating them by a comma.

CMS
Cool never knew that. When I wanted to limit say like a class sector so it would not search the entire page I just did something like $('#id .class')
chobo2