jQuery Selectors for Coding with JavaScript

By Chris Minnick, Eva Holland

Part of Coding with JavaScript For Dummies Cheat Sheet

JQuery provides many different ways to match sets of elements within a document beyond the methods built into JavaScript. Check out this list of all the jQuery selectors. To use them, simply pass them to the jQuery function (or, you can use the $ alias for the jQuery function). For example:

  • $(“*”) selects every element.

  • $(“div p:first-child”) selects the first paragraph child of each matched div element.

  • $(“div:contains(‘We hold these truths’)”) selects the divs that contain the matching text.

Check here to see examples of each of these jQuery selectors inside programs.

Selector What It Selects
All Selector (“*”) All elements.
:animated Selector All elements that are animated at the time of selection.
Attribute Contains Prefix Selector [name|=”value”] Elements that have the specified attribute with a value either
equal to a given string or starting with that string followed by a
hyphen (-).
Attribute Contains Selector [name*=”value”] Elements that have the specified attribute with a value
containing a given substring.
Attribute Contains Word Selector [name~=”value”] Elements that have the specified attribute with a value
containing a given word, delimited by spaces.
Attribute Ends With Selector [name$=”value”] Elements that have the specified attribute with a value ending
exactly with a given string. The comparison is case sensitive.
Attribute Equals Selector [name=”value”] Elements that have the specified attribute with a value exactly
equal to a certain value.
Attribute Not Equal Selector [name!=”value”] Elements that either don’t have the specified attribute
or do have the specified attribute but not with a certain
value.
Attribute Starts With Selector [name^=”value”] Elements that have the specified attribute with a value
beginning exactly with a given string.
:button Selector All button elements and elements of type button.
:checkbox Selector Elements of type checkbox.
:checked Selector All elements that are checked or selected.
Child Selector (“parent > child”) All direct child elements specified by “child” of
elements specified by “parent”.
Class Selector (“.class”) Elements with the given class.
:contains() Selector Elements that contain the specified text.
Descendant Selector (“ancestor descendant”) Elements that are descendants of a given ancestor.
:disabled Selector Elements that are disabled.
Element Selector (“element”) Elements with the given tag name.
:empty Selector Elements that have no children (including text nodes).
:enabled Selector Elements that are enabled.
:eq() Selector The element at index n within the matched set.
:even Selector Even elements, zero-indexed. See also odd.
:file Selector Elements of type file.
:first-child Selector Elements that are the first child of their parent.
:first-of-type Selector Elements that are the first among siblings of the same element
name.
:first Selector The first matched element.
:focus Selector The element that is currently focused.
:gt() Selector Elements at an index greater than index within the matched
set.
Has Attribute Selector [name] Elements that have the specified attribute, with any
value.
:has() Selector Elements which contain at least one element that matches the
specified selector.
:header Selector Elements that are headers, like h1, h2, h3, and so on.
:hidden Selector Elements that are hidden.
ID Selector (“#id”) A single element with the given id attribute.
:image Selector Elements of type image.
:input Selector All input, textarea, select, and button elements.
:lang() Selector Elements of the specified language.
:last-child Selector Elements that are the last child of their parent.
:last-of-type Selector Elements that are the last among siblings of the same element
name.
:last Selector The last matched element.
:lt() Selector Elements at an index less than index within the matched
set.
Multiple Attribute Selector [name=”value”][name2=”value2″] Elements that match all the specified attribute filters.
Multiple Selector (“selector1, selector2,
selectorN”)
The combined results of all the specified selectors.
Next Adjacent Selector (“prev + next”) All next elements matching “next” that are
immediately preceded by a sibling “prev”.
Next Siblings Selector (“prev ~ siblings”) All sibling elements that follow after the “prev”
element, have the same parent, and match the filtering
“siblings” selector.
:not() Selector Elements that do not match the given selector.
:nth-child() Selector Elements that are the nth-child of their parent.
:nth-last-child() Selector Elements that are the nth-child of their parent, counting from
the last element to the first.
:nth-last-of-type() Selector All the elements that are the nth-child of their parent in
relation to siblings with the same element name, counting from the
last element to the first.
:nth-of-type() Selector Elements that are the nth child of their parent in relation to
siblings with the same element name.
:odd Selector Odd elements, zero-indexed. See also even.
:only-child Selector Elements that are the only child of their parent.
:only-of-type Selector Elements that have no siblings with the same element name.
:parent Selector Elements that have at least one child node (either an element
or text).
:password Selector Elements of type password.
:radio Selector Elements of type radio.
:reset Selector Elements of type reset.
:root Selector The element that is the root of the document.
:selected Selector Elements that are selected.
:submit Selector Elements of type submit.
:target Selector The target element indicated by the fragment identifier of the
document’s URI.
:text Selector All input elements of type text.
:visible Selector Elements that are visible.