Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery selector

jQuery selector


jQuery selector

Please use our jQuery Selector Tester to demonstrate the different selectors.

elements, with index values starting from 0; the first element is even (0), the second is odd (1), and so on.elements, with index values starting from 0; the first element is even (0), the second is odd (1), and so on.
Selector Example Select
* $("*") all elements
#id $("#lastname") elements with id="lastname"
.class $(".intro") all elements with class="intro"
.class,.class $(".intro,.demo") all elements whose class is "intro" or "demo"
element $("p") all

Element

el1,el2,el3 $("h1,div,p") all

,
and

Element

:first $("p:first") first

Element

:last $("p:last") Last

Element

:even $("tr:even") all even
:odd $("tr:odd") all odd
:first-child $("p:first-child") all that are the first child of their parent

Element

:first-of-type $("p:first-of-type") the first ... of its parent

all ... elements

Element

:last-child $("p:last-child") all that are the last child of their parent

Element

:last-of-type $("p:last-of-type") the last ... of its parent

all ... elements

Element

:nth-child(n) $("p:nth-child(2)") all that are the second child of their parent

Element

:nth-last-child(n) $("p:nth-last-child(2)") all that are the second child of their parent

elements, counted starting from the last child

:nth-of-type(n) $("p:nth-of-type(2)") the second ... of its parent

all ... elements

Element

:nth-last-of-type(n) $("p:nth-last-of-type(2)") the second ... of its parent

all ... elements

elements, counted starting from the last child

:only-child $("p:only-child") all that are the only child of their parent

Element

:only-of-type $("p:only-of-type") all that are the only child of a specific type within their parent

Element

parent > child $("div > p")
all direct children of the ... element

Element

parent descendant $("div p")
all descendants of the ... element

Element

element + next $("div + p") Each
the one immediately after the ... element

Element

element ~ siblings $("div ~ p")
all siblings of the ... element

Element

:eq(index) $("ul li:eq(3)") the fourth element in the list (index starts from 0)
:gt(no) $("ul li:gt(3)") list elements with index greater than 3
:lt(no) $("ul li:lt(3)") list elements with index less than 3
:not(selector) $("input:not(:empty)") all non-empty input elements
:header $(":header") all heading elements

,

...

:animated $(":animated") all animated elements
:focus $(":focus") the element that currently has focus
:contains(text) $(":contains('Hello')") all elements containing the text "Hello"
:has(selector) $("div:has(p)") all that contain

an element within them

Element
:empty $(":empty") all empty elements
:parent $(":parent") Match all parent elements that contain child elements or text.
:hidden $("p:hidden") all hidden

Element

:visible $("table:visible") all visible tables
:root $(":root") the root element of the document
:lang(language) $("p:lang(de)") all with lang attribute value "de"

Element

     
[attribute] $("[href]") all elements with an href attribute
[attribute=value] $("[href='default.htm']") all elements with an href attribute whose value equals "default.htm"
[attribute!=value] $("[href!='default.htm']") all elements with an href attribute whose value does not equal "default.htm"
[attribute$=value] $("[href$='.jpg']") all elements with an href attribute whose value ends with ".jpg"
[attribute|=value] $("[title|='Tomorrow']") all elements with a title attribute whose value equals 'Tomorrow' or begins with the string 'Tomorrow' followed by a hyphen
[attribute^=value] $("[title^='Tom']") all elements with a title attribute whose value starts with "Tom"
[attribute~=value] $("[title~='hello']") all elements with a title attribute whose value contains the word "hello"
[attribute*=value] $("[title*='hello']") all elements with a title attribute whose value contains the string "hello"
[name=value][*name2*=*value2*] $( "input[id][name$='man']" ) input elements with an id attribute and a name attribute ending with "man"
     
:input $(":input") all input elements
:text $(":text") all input elements with type="text"
:password $(":password") all input elements with type="password"
:radio $(":radio") all input elements with type="radio"
:checkbox $(":checkbox") all input elements with type="checkbox"
:submit $(":submit") all input elements with type="submit"
:reset $(":reset") all input elements with type="reset"
:button $(":button") all input elements with type="button"
:image $(":image") all input elements with type="image"
:file $(":file") all input elements with type="file"
:enabled $(":enabled") all enabled elements
:disabled $(":disabled") all disabled elements
:selected $(":selected") all selected dropdown list elements
:checked $(":checked") all checked checkbox options
.selector $(selector).selector Deprecated in jQuery 1.7. Return the original selector passed to jQuery()
:target $( "p:target" ) the selector will select elements whose ID matches a formatted identifier in the URI

Element