This is a draft cheat sheet. It is a work in progress and is not finished yet.
Basic
"*" |
Finds all elements: $("*") |
"element" |
Finds all div elements: $("div"); |
"#id" |
Finds one element with an ID of "foo": $("#foo"); |
".class" |
Finds all elements with class name of "bar": $(".bar"); |
"s1,s2,sN" |
Find multiple elements: $("span, .left, .right, #fu"); |
Attributes
[n] |
All images that have a rel attribute: $("img[rel]"); |
[n!="v"] |
All inputs not of text type: $('input[type!="text"]'); |
[n="v"] |
All divs with data-age exactly equal to 3: $('div[data-age=3]'); |
[n*="v"] |
All elements containing id with 'box', e.g. 'xbox3': $('[id="box"]');* |
[n^="v"] |
All elements with an id starting with 'box': $('[id^="box"]'); |
[n$="v"] |
All elements with a title value ending in "name" ("lastname" & "firstname"): $('[title$="name"]'); |
[n|="v"] |
All p with a class name prefix of "x"; includes dashes 'x' & 'x-2', not 'x2': $('p[class|="x"]'); |
[n~="v"] |
All elements with a title word (wrapped by spaces) with an "x" like '1 x 2', not '1x2' or 1-x-2': $('p[title~="x"]'); |
[n="v"][a="b"] |
All inputs with multiple attributes of type text and a value containing "foo": $('input[type="text"][value*="foo"]'); |
el[n=v] el = element (optional);n = name;v = value;No space after element or between multiple attributes; Note the use of single and double quotes in the examples.
|
|
Basic Filters
:animated |
All animated divs: $("div:animated"); |
:eq(n) * |
p of index equals 1 (second): $("p:eq(1)"); |
:even * |
All even rows (1st, 3rd): $("tr:even"); |
:first |
Select first p: $("p:first"); same as $("p:eq(0)"); |
:gt(n) * |
All rows greater than second $("tr:gt(1)"); |
:header |
All header types (h1,h2...): $(":headers"); |
:last |
Select last div: $("div:last"); |
:lt(n) * |
All li's less than third: $("li:lt(2)"); |
:not(sel) |
All li's, just not the last: $("li:not(:last)"); |
:odd * |
All odd rows (2nd, 4th): $("tr:odd"); |
These zero-based selectors do not work with negative numbers. *
Child Filters
:first-child |
$("ul li:first-child"); Find all li's that are the first child of each ul |
:last-child |
$("table tr:last-child"); Find all last child tr's (last row) in every table |
:nth-child(i) |
$("div p:nth-child(4n)"); Every 4th (nth-child) p in matched div's (i=index, even, odd or equation;one-based) |
:only-child |
$("li:only-child"); Find all ol & ul with only one list item |
|
|
Forms
:button |
All button elements & input type=button: $(":button"); |
:checkbox |
All input type=checkbox: $("input:checkbox"); |
:checked |
All checked inputs: $("input:checked"); |
:disabled |
All disabled elements: $(":disabled"); |
:enabled |
All enabled elements: $(":enabled"); |
:file |
All input type=file: $("input:file"); |
:focus |
Currently focused element: $(":focus"); |
:image |
All input type=image: $("input:image"); |
:input |
All input, textarea, select & button: $(":input"); |
:password |
All input type=password: $("input:password"); |
:radio |
All input type=radio: $("input:radio"); |
:reset |
All input type=reset: $("input:reset"); |
:selected |
All selected options: $("select option:selected") |
:submit |
All input type=submit: $("input:submit"); |
:text |
All input type=text: $("input:text"); |
All of the form selectors will perform better if the element tag precedes the selector
|