jQuery Selector Reference

Introduction

Selector
Example
Selects
*
$("*")
all elements.
#id

$("#foo")

all elements
with the id="foo".
.class

$(".bar")

all elements
with the class="bar".
element
$("p")
all <p> elements.
selector1,
selector2,
selector_N
$("h1, p.bar, span")


all <h1> and <span>,
but only that <p> elements
that has the class="bar".
ancestor descendant

$("form label")

all <label> elements that are
descendant of their ancestor <form> element.
parent > child

$("ul > li")

all <li> elements that are
direct child of their parent <ul> element.
prev + next

$("h1 + p")

all <p> elements that are next i.e.
immediately preceded to the <h1> elements.
prev ~ siblings

$("h1 ~ p")

all <p> elements that are siblings and
follow after the <h1> elements.
:animated

$(":animated")

all elements that are animating at the
time the selector is run.
[attribute]

$("[href]")

all elements with a href attribute,
with any value.
[attribute="value"]

$('a[target="_blank"]')

all <a> elements that have the target
attribute with a value equal to "_blank".
[attribute|="value"]


$('a[hreflang|="en"]')


all <a> elements that have the hreflang
attribute with a value equal to "en", or
starting with "en" followed by a hyphen, like "en-US".
[attribute*="value"]

$('input[name*="male"]')

all <input> elements that have the name attribute with
a value containing the substring "male".
[attribute~="value"]

$('img[title~="My_Title"]')

all <img> elements that have the title attribute with a
value containing the word "My_Title", delimited by spaces.
[attribute$="value"]

$('img[src$=".png"]')

all <img> elements that have the src attribute with
a value ending with ".png".
[attribute!="value"]

$('a[target!="_blank"]')

all <a> elements that don't have the target attribute,
or if have don't with a value "_blank".
[attribute^="value"]

$('img[title^="My_Image"]')

all <img> elements that have the title attribute
with a value beginning exactly with a "My_Image" string.
:button

$(":button")

all input and button elements with
type="button".
:checkbox
$(":checkbox")
all input elements with type="checkbox".
:checked


$(":checked")


all elements that are checked or selected,
works for checkboxes, radio buttons, and
select elements.
:contains()
$('p:contains("Hello")')
all <p> elements that contains the text "Hello".
:disabled
$(":disabled")
all elements that are disabled.
:empty

$("td:empty")

all <td> elements that are empty i.e that have
no children including text.
:enabled
$(":enabled")
all elements that are enabled.
:eq()

$("tr:eq(1)")

the 2nd <tr> element within the matched set, zero-based
index.
:even
$("tr:even")
all even <tr> elements, zero-indexed.
:file
$(":file")
all input elements with type="file".
:first-child

$("p:first-child")

all <p> elements that are the first child of
their parent.
:first-of-type

$("p:first-of-type")

all <p> elements that are the first <p> element of
their parent.
:first
$("p:first")
the first <p> element.
:focus
$(":focus")
element if it is currently focused.
:gt()


$("ul li:gt(3)")


all <li> elements at an index greater than three
within the matched set (i.e. selects 4th, 5th, ...
list elements), zero-based index.
:has()

$("p:has(img)")

all <p> elements which contain at least one
<img> element.
:header

$(":header")

all elements that are headers, like <h1>, <h2>,
<h3> and so on.
:hidden
$("p:hidden")
all <p> elements that are hidden.
:image
$(":image")
all input elements with type="image".
:input

$(":input")

all input, textarea, select and button
elements, all form controls.
:lang()

$(":lang(en)")

all elements that have a language value "en" i.e.
lang="en", lang="en-us" etc.
:last-child
$("p:last-child")
all <p> elements that are the last child of their parent.
:last-of-type

$("p:last-of-type")

all <p> elements that are the last <p> element of their
parent.
:last
$("p:last")
the last <p> element.
:lt()


$("ul li:lt(3)")


all <li> elements at an index less than three within the
matched set (i.e. selects 1st, 2nd, 3rd list elements),
zero-based index.
:not()
$("p:not(:empty)")
all <p> elements that are not empty.
:nth-child(n)

$("p:nth-child(3)")

all <p> elements that are the
3rd child of their parent.
:nth-last-child(n)


$("p:nth-last-child(3)")


all <p> elements that are the
3rd-child of their parent, counting from the last
element to the first.
:nth-last-of-type(n)

$("p:nth-last-of-type(2)")

all <p> elements that are the 2nd-child of their parent,
counting from the last element to the first.
:nth-of-type(n)

$("p:nth-of-type(2)")

all <p> elements that are the 2nd <p> element of
their parent
:odd
$("tr:odd")
all odd <tr> elements, zero-indexed.
:only-child
$("p:only-child")
all <p> elements that are the only child of their parent.
:only-of-type

$("p:only-of-type")

all <p> elements that have no siblings with the same
element name.
:parent

$(":parent")

all elements that have at least one child node either
an element or text.
:password
$(":password")
all input elements with type="password".
:radio
$(":radio")
all input elements with type="radio".
:reset
$(":reset")
all input and button elements with type="reset".
:root

$(":root")

the document's root element which is always <html>
element in a HTML document.
:selected

$(":selected")

all elements that are selected, only works
for <option> elements.
:submit
$(":submit")
all input and button elements with type="submit".
:text
$(":text")
all input elements with type="text".
:visible
$("p:visible")
all <p> elements that are visible.



PreviousNext

Related