Selector Grammar

The jQuery selector grammar is very similar to that of CSS3, and it is explained in detail in jQuery Selectors. The following is a summary:

Simple tag, class, and id selectors
*            tagname       .classname     #id
Selector combinations
A B    B as a descendant of A
A > B  B as a child of A
A + B  B as a sibling following A
A ~ B  B as a sibling of A
Attribute filters
[attr]       has attribute
[attr=val]   has attribute with value val
[attr!=val]  does not have attribute with value val
[attr^=val]  attribute begins with val
[attr$=val]  attribute ends with val
[attr*=val]  attribute includes val
[attr~=val]  attribute includes val as a word
[attr|=val]  attribute begins with val and optional hyphen
Element type filters
:button      :header       :password      :submit
:checkbox    :image        :radio         :text
:file        :input        :reset
Element state filters
:animated    :disabled     :hidden        :visible
:checked     :enabled      :selected
Selection position filters
:eq(n)       :first        :last          :nth(n)
:even        :gt(n)        :lt(n)         :odd
Document position filters
:first-child               :nth-child(n)
:last-child                :nth-child(even)
:only-child                :nth-child(odd)
                           :nth-child(xn+y)
Miscellaneous filters
:contains(text)            :not(selector)
:empty                     :parent
:has(selector)

Get jQuery Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.