This is an old version of the Selectors API:
View the Current API

jQuery selectors are a combination of CSS 1-3, XPath, plus some custom code to glue it together. Essentially, the best parts from both of these query languages were taken, combined, and used to create the final jQuery expression language. If you already know CSS (which most web developers do) then you're going to be fine.

If you are unsure about the general differences between the techniques discussed here, these articles in the English Wikipedia may help clarify a lot: XPath and [

Contents

[edit]

Using CSS and XPath Together

This is a point of confusion, for some: How can you use CSS and XPath together, they're so different! jQuery makes some allowances to make this happen, but we think that developers will really appreciate the advantages of each language. Here are some examples:

Hide all Paragraph elements that contain a class attribute:

 $("p[@class]").hide();

Show the first paragraph on the page:

 $("p:eq(0)").show();

Hide all divs that are currently showing:

 $("div:visible").hide();

Get all list items that are children of an unordered list:

 $("ul/li")
 /* valid too: $("ul > li") */

Get all Paragraphs, with a class of 'foo', that have a link in them:

 $("p.foo[a]");

Get list item that contains link with "Register" text inside:

 $("li[a:contains('Register')]");

Get the input field's value with the name of 'bar':

 $("input[@name=bar]").val();

All checked radio buttons:

 $("input[@type=radio][@checked]")

If you still have questions concerning how this selector language works, please feel free to post to the mailing list.

[edit]

CSS Selectors

jQuery has full CSS 1-2 support and partial CSS 3 support, along with some custom CSS-like functionality (and XPath), as part of its expression.

For info on how CSS works feel free to read the various links:

What follows is a list of supported CSS Selector expressions.

[edit]

Supported, but different

All attribute selectors are written like their XPath counter-parts (in that all attributes should begin with an @ symbol).

[edit]

Not supported

jQuery only supports selectors that actually select DOM elements - everything else is ignored.

jQuery doesn't support the following selectors due to their lack of real-world usefulness:

[edit]

Context and Anchoring

In jQuery, unlike real CSS, a selector expression may have a context other than the entire document, for instance with the function $(expr, context). You can anchor a CSS-like expression to the context root by starting it with one of the selectors >, +, or ~.

[edit]

XPath Selectors

One of the selector languages that jQuery supports, as a part of its expression language is XPath. jQuery supports basic XPath expressions, in addition to CSS 1-3. Here are some samples:

[edit]

Location Paths

 $("/html/body//p")
 $("body//p")
 $("p/../div")
 $("p/*", this)
 $("/p//a", this)
[edit]

Supported Axis Selectors

 $("//div//p")
 $("//div/p")
 $("//div ~ form")
 $("//div/../p")
[edit]

Supported Predicates

 $("//input[@checked]")
 $("//a[@ref='nofollow']")
 $("//div[p]")
 $("//div[p/a]")
[edit]

Supported Predicates, but differently

 $("p:last")
 $("p:first")
 $("p:eq(0)")
 $("p:lt(5)")
 $("p:gt(2)")
[edit]

Custom Selectors

jQuery includes some expressions that aren't available in either CSS or XPath, but were found to be very handy, so were included.

The following expressions' syntax is based upon the various CSS selectors, of similar names.

[edit]

Custom Selectors

Some examples:

 $("p:first").css("fontWeight","bold");
 $("div:hidden").show();
 $("/div:contains('test')", this).hide();
[edit]

Form Selectors

There are a few selectors for form elements:

Also available is :hidden, see the description above for details.

It is recommended to provide a context when using the above:

 $('#myForm :input')

Or, if you have already a reference to your form:

 $('input:radio', myForm)

This would select all input elements of type radio inside myForm. Using :radio is mostly the same as [@type=radio], but should be slightly faster. Good to know when working with large forms.

[edit]

More Selectors

The jQuery selector system can be extended by third parties:

[edit]

See Also