
CSS selectors
The following selectors are based on the Cascading Style Sheet specifications (1–3), as outlined by the W3C. For more information about the specifications, visit http://www.w3.org/Style/CSS/#specs.
Element (T)
Select all elements that have a tag name of T
.
Examples
$('div')
selects all elements with a tag name ofdiv
in the document$('em')
selects all elements with a tag name ofem
in the document
Description
JavaScript's getElementsByTagName()
function is called to return the appropriate elements when this expression is used.
ID (#myid)
Select the unique element with an ID equal to myid
.
Examples
$('#myid')
selects the unique element withid="myid"
$('p#myid')
selects a single paragraph with an ID ofmyid
; in other words, the unique element<p id="myid">
Description
Each ID value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. However, this behavior should not be relied on. A document with more than one element using the same ID is invalid.
In our second example, it might not be immediately clear why someone might want to specify a tag name associated with a particular ID, as that id
value needs to be unique anyway. However, some situations in which parts of the DOM are user-generated may require a more specific expression to avoid false positives. Furthermore, when the same script is run on more than one page, it might be necessary to identify the element ID as the pages could be associating the same ID with different elements. For example, page A might have <h1 id="title">
while page B has <h2 id="title">
.
For ID selectors such as the preceding examples, jQuery uses the JavaScript function getElementById()
, which is extremely efficient. When another selector is attached to the ID selector, like in the second example, jQuery performs an additional check before identifying the element as a match.
Note
As always, remember that as a developer, your time is typically the most valuable resource. Do not focus on optimization of selector speed unless it is clear that performance needs to be improved.
Class (.myclass)
Select all elements that have a class of myclass
.
Examples
$('.myclass')
selects all elements that have a class ofmyclass
$('p.myclass')
selects all paragraphs that have a class ofmyclass
$('.myclass.otherclass')
selects all elements that have a class of bothmyclass
andotherclass
Description
For class selectors, jQuery uses JavaScript's native getElementsByClassName()
function if the browser supports it. Otherwise, it checks the .className
attribute of each element.
As a CSS selector (for example, in a stylesheet), the multiple-class syntax used in the third example is supported by all modern web browsers, but not by Internet Explorer versions 6 and below. This makes the syntax especially handy for applying styles cross-browser through jQuery.
Descendant (E F)
Select all elements matched by F
that are descendants of an element matched by E
.
Examples
$('#container p')
selects all paragraph elements that are descendants of an element that has an ID ofcontainer
$('a img')
selects all<img>
elements that are descendants of an<a>
element
Description
Descendants of an element are that element's children, grandchildren, great-grandchildren, and so on. For example, in the following HTML code, the <img>
element is a descendant of the <span>
, <p>
, <div id="inner">
, and <div id="container">
elements:
<div id="container"> <div id="inner"> <p> <span><img src="example.jpg" alt="" /></span> </p> </div> </div>
Child (E > F)
Select all elements matched by F
that are children of an element matched by E
.
Examples
$('li > ul')
selects all<ul>
elements that are children of an<li>
element$('.myclass > code')
selects all<code>
elements that are children of an element with the classmyclass
Description
As a CSS selector, the child combinator is supported by all modern web browsers including Safari, Mozilla/Firefox, Opera, Chrome, and Internet Explorer 7 and above; but notably not by Internet Explorer versions 6 and below. The first example is a handy way of selecting all nested unordered lists (that is, except the top level), and jQuery makes it possible to do this in a cross-browser fashion.
The child combinator (E > F) can be thought of as a more specific form of the descendant combinator (E F) in that it selects only first-level descendants. Therefore, in the following HTML code, the <img>
element is a child only of the <span>
element:
<div id="container"> <div id="inner"> <p> <span><img src="example.jpg" alt="" /></span> </p> </div> </div>
Adjacent sibling (E + F)
Select all elements matched by F
that immediately follow and have the same parent as an element matched by E
.
Examples
$('ul + p')
selects all<p>
elements that immediately follow a sibling<ul>
element$('#myid + .myclass')
selects the element withclass="myclass"
that immediately follows a sibling element withid="myid"
Description
One important point to consider with both the adjacent sibling combinator (E + F) and the general sibling combinator (E ~ F, covered next) is that they only select siblings. Consider the following HTML code:
<div id="container"> <ul> <li></li> <li></li> </ul> <p> <img/> </p> </div>
$('ul + p')
selects<p>
because it immediately follows<ul>
and the two elements share the same parent,<div id="container">
$('p + img')
selects nothing because<p>
is one level higher in the DOM tree than<img>
$('li + img')
selects nothing because even though<li>
and<img>
are on the same level in the DOM tree, they do not share the same parent
General sibling (E ~ F)
Select all elements matched by F
that follow and have the same parent as an element matched by E
.
Examples
$('ul ~ p')
selects all<p>
elements that follow a sibling<ul>
element$('#myid ~ .myclass')
selects all elements withclass="myclass"
that follow a sibling element withid="myid"
Description
One important point to consider with both the adjacent sibling combinator (E + F) and the general sibling combinator (E ~ F) is that they only select siblings. The notable difference between the two is their respective reach. While the former reaches only to the immediately following sibling element, the latter extends that reach to all of the following sibling elements.
Consider the following HTML code:
<ul> <li class="first"></li> <li class="second"></li> <li class="third></li> </ul> <ul> <li class="fourth"></li> <li class="fifth"></li> <li class="sixth"></li> </ul>
$('li.first ~ li')
selects<li class="second">
and<li class="third">
$('li.first + li')
selects<li class="second">
Multiple expressions (E, F, G)
Select all elements matched by any of the selector expressions E
, F
, or G
.
Examples
$('code, em, strong')
selects all<code>
,<em>
, and<strong>
elements$('p strong, .myclass')
selects all<strong>
elements that are descendants of a<p>
element, as well as all elements that have a class ofmyclass
Description
This multiple expression combinator is an efficient way to select disparate elements. An alternative to this combinator is the .add()
method described in Chapter 3, DOM Traversal Methods.
Numbered child (:nth-child(n/even/odd/expr))
Select all elements that are the nth child of their parent.
Examples
$('li:nth-child(2)')
selects all<li>
elements that are the second child of their parent$('p:nth-child(odd)')
selects all<p>
elements that are an odd-numbered child of their parent (first, third, fifth, and so on)$('.myclass:nth-child(3n+2)')
selects all elements with the classmyclass
that are the (3n+2)th child of their parent (second, fifth, eighth, and so on)
Description
As jQuery's implementation of :nth-child(n)
is strictly derived from the CSS specification, the value of n
is "1-based," meaning that the counting starts at 1. However, for all other selector expressions, jQuery follows JavaScript's "0-based" counting. Therefore, given a single <ul>
containing two <li>
s, $('li:nth-child(1)')
selects the first <li>
while $('li:eq(1)')
selects the second.
As the two look so similar, the :nth-child(n)
pseudo-class is easily confused with :nth(n)
, a synonym for :eq(n)
. However, as we have just seen, the two can result in dramatically different matched elements. With :nth-child(n)
, all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With :nth(n)
, only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the nth one is selected. To demonstrate this distinction, let's examine the results of a few selector expressions given the following HTML code:
<div> <h2></h2> <p></p> <h2></h2> <p></p> <p></p> </div>
$('p:nth(1)')
selects the second<p>
because numbering for:nth(n)
starts with0
$('p:nth-child(1)')
selects nothing because there is no<p>
element that is the first child of its parent$('p:nth(2)')
selects the third<p>
$('p:nth-child(2)')
selects the first<p>
because it is the second child of its parent
In addition to taking an integer, :nth-child(n)
can take even
or odd
. This makes it especially useful for table-row striping solutions when more than one table appears in a document. Again, given the preceding HTML snippet:
$('p:nth-child(even)')
selects the first and third<p>
because they are children 2 and 4 (both even numbers) of their parent
The third example selector illustrates the most complicated usage of :nth-child()
. When a simple mathematical expression of the form an+b
is provided with integers substituted for a
and b
, :nth-child()
selects the elements whose positions in the list of children are equal to an+b
for some integer value of n
.
Further description of this unusual usage can be found in the W3C CSS specification at http://www.w3.org/TR/css3-selectors/#nth-child-pseudo.
First child (:first-child)
Select all elements that are the first child of their parent element.
Examples
$('li:first-child')
selects all<li>
elements that are the first child of their parent element$(.myclass:first-child')
selects all elements with the classmyclass
that are the first child of their parent element
Description
The :first-child
pseudo-class is shorthand for :nth-child(1)
. For more information on :X-child
pseudo-classes, see the Description for the Numbered child selector.
Last child (:last-child)
Select all elements that are the last child of their parent element.
Examples
$('li:last-child')
selects all<li>
elements that are the last child of their parent element$(.myclass:last-child')
selects all elements with the classmyclass
that are the last child of their parent element
Description
For more information on :X-child
pseudo-classes, see the Description for the Numbered child selector.
Only child (:only-child)
Select all elements that are the only child of their parent element.
Examples
$(':only-child')
selects all elements in the document that are the only child of their parent element$('code:only-child')
selects all<code>
elements that are the only child of their parent element
Not (:not(E))
Select all elements that do not match the selector expression E
.
Examples
$(':not(.myclass)')
selects all elements in the document that do not have the classmyclass
$('li:not(:last-child)')
selects all<li>
elements that are not the last child of their parent elements
Empty (:empty)
Select all elements that have no children (including text nodes).
Examples
$(':empty')
selects all elements in the document that have no children$('p:empty')
selects all<p>
elements that have no children
Description
One important thing to note with :empty
(and :parent
) is that child elements include text nodes.
The W3C recommends that the <p>
element should have at least one child node, even if that child is merely text (see http://www.w3.org/TR/html401/struct/text.html#edef-P). On the other hand, some other elements are empty (that is, have no children) by definition; for example, <input>
, <img>
, <br>
, and <hr>
.
Universal (*)
Select all elements.
Examples
$('*')
selects all elements in the document$('p > *')
selects all elements that are children of a paragraph element
Description
The universal
selector is especially useful when combined with other expressions to form a more specific selector expression.