The use of jQuery Selectors Tester to demonstrate the different selectors
Selector | Example | Selects |
---|---|---|
* | $("*") | All elements |
#id | $("#lastname") | The element with id="lastname" |
.class | $(".intro") | All elements with class="intro" |
.class,.class | $(".intro,.demo") | All elements with the class "intro" or "demo" |
element | $("p") | All <p> elements |
el1,el2,el3 | $("h1,div,p") | All <h1>, <div> and <p> elements |
:first | $("p:first") | The first <p> element |
:last | $("p:last") | The last <p> element |
:even | $("tr:even") | All even <tr> elements |
:odd | $("tr:odd") | All odd <tr> elements |
: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 |
: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 |
:nth-child(n) | $("p:nth-child(2)") | All <p> elements that are the 2nd child of their parent |
:nth-last-child(n) | $("p:nth-last-child(2)") | All <p> elements that are the 2nd child of their parent, counting from the last child |
:nth-of-type(n) | $("p:nth-of-type(2)") | All <p> elements that are the 2nd <p> element of their parent |
:nth-last-of-type(n) | $("p:nth-last-of-type(2)") | All <p> elements that are the 2nd <p> element of their parent, counting from the last child |
: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 are the only child, of its type, of their parent |
parent > child | $("div > p") | All <p> elements that are a direct child of a <div> element |
parent descendant | $("div p") | All <p> elements that are descendants of a <div> element |
element + next | $("div + p") | The <p> element that are next to each <div> elements |
element ~ siblings | $("div ~ p") | All <p> elements that appear after the <div> element |
:eq(index) | $("ul li:eq(3)") | The fourth element in a list (index starts at 0) |
:gt(no) | $("ul li:gt(3)") | List elements with an index greater than 3 |
:lt(no) | $("ul li:lt(3)") | List elements with an index less than 3 |
:not(selector) | $("input:not(:empty)") | All input elements that are not empty |
:header | $(":header") | All header elements <h1>, <h2> ... |
:animated | $(":animated") | All animated elements |
:focus | $(":focus") | The element that currently has focus |
:contains(text) | $(":contains('Hello')") | All elements which contains the text "Hello" |
:has(selector) | $("div:has(p)") | All <div> elements that have a <p> element |
:empty | $(":empty") | All elements that are empty |
:parent | $(":parent") | All elements that are a parent of another element |
:hidden | $("p:hidden") | All hidden <p> elements |
:visible | $("table:visible") | All visible tables |
:root | $(":root") | The document's root element |
:lang(language) | $("p:lang(de)") | All <p> elements with a lang attribute value starting with "de" |
[attribute] | $("[href]") | All elements with a href attribute |
[attribute=value] | $("[href='default.htm']") | All elements with a href attribute value equal to "default.htm" |
[attribute!=value] | $("[href!='default.htm']") | All elements with a href attribute value not equal to "default.htm" |
[attribute$=value] | $("[href$='.jpg']") | All elements with a href attribute value ending with ".jpg" |
[attribute|=value] | $("[title|='Tomorrow']") | All elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen |
[attribute^=value] | $("[title^='Tom']") | All elements with a title attribute value starting with "Tom" |
[attribute~=value] | $("[title~='hello']") | All elements with a title attribute value containing the specific word "hello" |
[attribute*=value] | $("[title*='hello']") | All elements with a title attribute value containing the word "hello" |
: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 input elements |
:disabled | $(":disabled") | All disabled input elements |
:selected | $(":selected") | All selected input elements |
:checked | $(":checked") | All checked input elements |
The * selector selects all elements in the document, including html, head and body.
If the * selector is used together with another element, it selects all child elements within the specified element.
number
$("*")
Tip : The * selector can be heavy to process for some browsers.
The #id selector selects the element with the specific id.
The id refers to the id attribute of an HTML element.
Note: The id attribute must be unique within a document.
Do not start an id attribute with a number. It may cause problems in some browsers.
$("#id")
Parameter | Description |
---|---|
id | Required. Specifies the id of the element to select |
The .class selector selects all elements with the specific class.
The class refers to the class attribute of an HTML element.
The class attribute is used to set a particular style for several HTML elements.
Note: Do not start a class attribute with a number. It may cause problems in some browsers.
$(".class")
Parameter | Description |
---|---|
class | Required. Specifies the class of the elements to select |
The .class selector can also be used to select multiple classes.
Note: Seperate each class with a comma.
Do not start a class attribute with a number. It may cause problems in some browsers.
Parameter | Description |
---|---|
class | Required. Specifies the class of the elements to select |
$(".class1,.class2,.class3,...")
Select all elements with class "intro", "demo" or "end":
The element selector selects all elements with the specific element name.
$("element")
Parameter | Description |
---|---|
element | Required. Specifies the element to select |
The element selector can also be used to select multiple elements.
Note: Seperate each element with a comma.
$("element1,element2,element3,...")
Parameter | Description |
---|---|
element | Required. Specifies the elements to be selected |
The :first selector selects the first element.
This is mostly used together with another selector to select the first element in a group (like in the example above).
Note: This selector can only select one single element. Use the :first-child selector to select more than one element (one for each parent).
$(":first")
The :last selector selects the last element.
This is mostly used together with another selector to select the last element in a group (like in the example above).
Note: This selector can only select one single element. Use the :last-child selector to select more than one element (one for each parent).
$(":last")
The :even selector selects each element with an even index number (like: 0, 2, 4, etc.).
The index numbers start at 0.
This is mostly used together with another selector to select every even indexed element in a group (like in the example above).
$(":even")
The :odd selector selects each element with an odd index number (like: 1, 3, 5, etc.).
The index numbers start at 0.
This is mostly used together with another selector to select every odd indexed element in a group (like in the example above).
$(":odd")
The :first-child selector selects all elements that are the first child of their parent.
$(":first-child")
Select every <p> element that is the first child of its parent:
The :first-of-type selector selects all elements that are the first child, of a particular type, of their parent.
This is the same as :nth-of-type(1).
Use the :last-of-type selector to select all elements that are the last child, of a particular type, of their parent.
$(":first-of-type")
Select every <p> element that is the first <p> element of its parent:
The :last-child selector selects all elements that are the last child of their parent.
Use the :first-child selector to select elements that are the first child of their parent.
$(":last-child")
Select every <p> element that is the last child of its parent:
The :last-of-type selector selects all elements that are the last child, of a particular type, of their parent.
$(":last-of-type")
Select every <p> element that is the last <p> element of its parent:
The :nth-child(n) selector selects all elements that are the nth child, regardless of type, of their parent.
Note: Use the :nth-of-type() selector to select all elements that are the nth child, of a particular type, of their parent.
:nth-child(n|even|odd|formula)
Parameter | Description |
---|---|
n | The index of each child to match. Must be a number. The first element has the index number 1. |
even | Selects each even child element |
odd | Selects each odd child element |
formula | Specifies which child element(s) to be selected with a formula (an + b).
Example: p:nth-child(3n+2) selects each 3rd paragraph, starting at the 2nd child element |
Select each <p> element that is the third child of its parent:
The :nth-last-child(n) selector selects all elements that are the nth child, regardless of type, of their parent, counting from the last child.
Note: Use the :nth-last-of-type() selector to select all elements that are the nth child, of a particular type, of their parent, counting from the last child.
:nth-last-child(n|even|odd|formula)
Parameter | Description |
---|---|
n | The index of each child to match. Must be a number. The first element has the index number 1. |
even | Selects each even child element |
odd | Selects each odd child element |
formula | Specifies which child element(s) to be selected with a formula (an + b). Example: p:nth-last-child(3n+2) selects each 3rd paragraph, starting at the last 2nd child |
Select each <p> element that is the third child of its parent, counting from the last child:
The :nth-of-type(n) selector selects all elements that are the nth child, of a particular type, of their parent.
Note: Use the :nth-child() selector to select all elements that are the nth child, regardless of type, of their parent.
:nth-of-type(n|even|odd|formula)
Parameter | Description |
---|---|
n | The index of each child to match. Must be a number. The first element has the index number 1. |
even | Selects each even child element |
odd | Selects each odd child element |
formula | Specifies which child element(s) to be selected with a formula (an + b). Example: p:nth-of-type(3n+2) selects each 3rd paragraph, starting at the 2nd paragraph |
Select each <p> element that is the third <p> element of its parent:
The :nth-last-of-type(n) selector selects all elements that are the nth child, of a particular type, of their parent, counting from the last child.
Use the :nth-last-child() selector to select all elements that are the nth child, regardless of type, of their parent, counting from the last child.
:nth-last-of-type(n|even|odd|formula)
Parameter | Description |
---|---|
n | The index of each child to match. Must be a number. The first element has the index number 1. |
even | Selects each even child element |
odd | Selects each odd child element |
formula | Specifies which child element(s) to be selected with a formula (an + b). Example: p:nth-last-child(3n+2) selects each 3rd paragraph, starting at the last 2nd paragraph |
Select each <p> element that is the third <p> element of its parent, counting from the last child:
The :only-child selector selects every element that is the only child of its parent.
$(":only-child")
Select each <p> element that is the only child of its parent:
The :only-of-type selector selects every element that is the only child of its type, of its parent.
$(":only-of-type")
Select each <p> element that is the only <p> element of its parent:
The ("parent > child") selector selects all elements that are a direct child of the specified element.
("parent > child")
Parameter | Description |
---|---|
parent | Required. Specifies the parent element to be selected |
child | Required. Specifies the direct child element (of the specified parent element) to be selected |
Select all <span> elements that are a direct child of a <div> element:
The ("parent descendant") selector selects all elements that are descendants of a specified element.
A descendant of an element could be a child, grandchild, great-grandchild, etc, of that element.
("parent descendant")
Parameter | Description |
---|---|
parent | Required. Specifies the parent element to be selected |
descendant | Required. Specifies the descendant element (of the specified parent element) to be selected |
Select all <span> elements that are descendants of a <div> element:
The ("element + next") selector selects the "next" element of the specified "element". The "next" element must be placed right after the specified "element" to be selected.
Example: If you have two <p> elements right after a <div> element, this syntax:
Example: If you have an <h2> element right after a <div> element, and then a <p> element, this syntax:
Note: Both of the specified elements must share the same parent.
("element + next")
Parameter | Description |
---|---|
element |
Required.
Any valid jQuery selector |
next | Required. Specifies the element that should be the next element of the element parameter |
The ("element ~ siblings") selector selects sibling elements that appear after the specified "element".
Both of the specified elements must share the same parent.
("element ~ siblings")
Parameter | Description |
---|---|
element | Required. Any valid jQuery selector |
siblings | Required. Specifies the siblings of the element parameter |
Select all <p> elements that are siblings and appear after the <div> element:
The :eq() selector selects an element with a specific index number.
The index numbers start at 0, so the first element will have the index number 0 (not 1).
This is mostly used together with another selector to select a specifically indexed element in a group (like in the example above).
$(":eq(index)")
Parameter | Description |
---|---|
index | Required. Specifies the index of the element |
The :gt() selector selects elements with an index number higher than a specified number.
The index numbers start at 0.
This is mostly used together with another selector to select the last elements in a group (like in the example above)
$(":gt(index)")
Parameter | Description |
---|---|
index | Required. Specifies which element to select. Elements with an index higher than the specified number is selected. |
The :lt() selector selects elements with an index number less than a specified number.
The index numbers start at 0.
This is mostly used together with another selector to select the first elements in a group (like in the example above).
$(":lt(index)")
Parameter | Description |
---|---|
index | Required. Specifies which element to select. Elements with an index lesser than the specified number is selected. |
The :not() selector selects all elements except the specified element.
This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).
$(":not(selector)")
Parameter | Description |
---|---|
selector | Required. Specifies the element to not select. This parameter accepts any kind of selector |
The :header selector selects all header elements (<h1> to <h6>).
$(":header")
The :animated selector selects all elements that are currently animated.
$(":animated")
The :focus selector selects the element that currently has focus.
This selector is often used with a tag name or another selector, if not, this selector will be the same as ("*:focus").
$(":focus")
The :contains() selector selects elements containing the specified string.
The string can be contained directly in the element as text, or in a child element.
This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).
$(":contains(text)")
Parameter | Description |
---|---|
text | Required. Specifies the text to find |
The :has() selector selects all elements that have one or more elements inside of them, that matches the specified selector.
To select an element that have multiple elements inside of it, use comma (see example below).
$(":has(selector)")
Parameter | Description |
---|---|
selector | Required. Specifies the element to select. This parameter accepts any kind of selector |
Select all <p> elements that have a <span> element inside of them:
The :empty selector selects empty elements.
An empty element is an element without child elements or text.
$(":empty")
The :parent selector selects all elements that are the parent of another element, including text nodes.
$(":parent")
The :hidden selector selects hidden elements.
Hidden elements are elements that are:
$(":hidden")
The :visible selector selects every element that is currently visible.
Visible elements are elements that are not:
$(":visible")
The :root selector selects the document's root element.
In HTML, the root element is always the <html> element.
$(":root")
Set the background color for the HTML document to yellow:
The :lang() selector selects all elements with the language attribute starting with a specified value.
Note: The value has to be a whole word, either alone, like lang="en", or followed by a hyphen( - ), like lang="en-us".
$(":lang(language)")
Select all <p> elements with a lang attribute value that starts with "it":
The [attribute] selector selects each element with the specified attribute.
$("[attribute]")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
The [attribute=value] selector selects each element with the specified attribute and value.
$("[attribute=value]")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the value to find |
Select every element containing an id attribute with the value "choose":
The [attribute!=value] selector selects each element that does NOT have the specified attribute and value.
Elements with the selected attribute, but with a different value, will be selected.
$("[attribute!='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the value to find |
Select all <p> elements NOT containing a class attribute with the value "intro":
The [attribute$=value] selector selects each element with a specific attribute, with a value ending in a specific string.
number
$("[attribute$='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the string the value should end with |
Select all <a> elements with a href attribute that ends with ".org":
The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us").
$("[attribute|='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the string the attribute value should start with |
Select all <p> elements with a title attribute that starts with the value "Tomorrow":
The [attribute^=value] selector selects each element with a specific attribute, with a value beginning in a specific string.
number
$("[attribute^='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the string the value should begin with |
Select all <input> elements with a name attribute that starts with "nation":
The [attribute~=value] selector selects each element with a specific attribute, with a value containing a specific string.
The string can contain whitespace.
$("[attribute~='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the string value |
Select all <input> elements with a name attribute that contains the specific word "nation":
The [attribute*=value] selector selects each element with a specific attribute, with a value containing a string.
$("[attribute*='value']")
Parameter | Description |
---|---|
attribute | Required. Specifies the attribute to find |
value | Required. Specifies the string value |
Select all <input> elements with a name attribute that contains the word "nation":
The :input selector selects form elements.
This selector also works with the button element.
$(":input")
The :text selector selects input elements with type=text.
$(":text")
The :password selector selects input elements with type=password.
$(":password")
The :radio selector selects input elements with type=radio.
$(":radio")
The :checkbox selector selects input elements with type=checkbox.
$(":checkbox")
The :submit selector selects button and input elements with type=submit.
If a button element has no defined type, most browsers will use it as a button with type=submit.
$(":submit")
The :reset selector selects button and input elements with type=reset.
Using input:reset as a selector will not select the button element.
$(":reset")
The :button selector selects button elements, and input elements with type=button.
Using input:button as a selector will not select the button element.
Select <button> elements and <input> elements with type="button":
The :image selector selects input elements with type=image.
$(":image")
The :file selector selects input elements with type=file.
$(":file")
The :enabled selector selects all enabled form elements.
$(":enabled")
The :disabled selector selects all disabled form elements.
$(":disabled")
The :selected selector selects option elements that are pre-selected.
Note: This selector will not work on checkboxes or radio buttons. Use the :checked selector instead.
$(":selected")
The :checked selector selects all checked checkboxes or radio buttons.
$(":checked")
We will learn about event references in next chapter Click