jQuery Introduction | Selecting Elements | jQuery Selectors

jQuery Selectors

In jQuery, you can use not only CSS selectors but also some non-standard selectors. These non-standard selectors let you store selected elements or filter the selected result.

Storing Selected Elements in a Variable

In jQuery, selected elements can be stored in a variable and reused.

When elements exist as follows:

<ul>
    <li>Espresso</li>
    <li>Americano</li>
    <li>Caffe latte</li>
</ul>

The following example selects all <li> elements in the document, stores them in a variable, and uses that variable to get the number of elements.

var coffees = $("li");
$("#length").text("There are " + coffees.length + " kinds of coffee in total.");

Run code

Elements stored this way are only the elements that existed at the time they were stored in the variable. Therefore, elements added or removed after the elements are stored are not automatically reflected.

Filtering Selected Elements

In jQuery, you can filter selected elements to make a more detailed selection.

When elements exist as follows:

<ul>
    <li>Espresso</li>
    <li><b>Americano</b></li>
    <li>Caffe latte</li>
</ul>

The following example selects only the <li> elements in the document that contain a <b> element and adds text to them.

$("li:has(b)").append(" - Recommended menu.");

Run code

Selectors that can be used for filtering are as follows.

Selector Example Description
:eq(n) $("ul li:eq(3)") Selects the selected element whose index is n.
:gt(n) $("ul li:gt(3)") Selects all selected elements whose index is greater than n.
:lt(n) $("ul li:lt(3)") Selects all selected elements whose index is less than n.
:even $("tr:even") Selects all selected elements whose index is even.
:odd $("tr:odd") Selects all selected elements whose index is odd.
:first $("p:first") Selects the first selected element.
:last $("p:last") Selects the last selected element.
:animated $(":animated") Selects all selected elements whose animation effect is currently running.
:header $(":header") Selects all elements from <h1> through <h6>.
:lang(language) $("p:lang(en)") Selects all selected elements in the specified language.
:not(selector) $("input:not(:empty)") Selects all selected elements that do not match the specified selector.
:root $(":root") Selects the top-level root element.
target $("a[target='_blank']") Selects all selected elements that match the fragment identifier of the web page URI.
:contains(text) $(":contains('Hello')") Selects all selected elements that contain the specified text.
:has(selector) $("p:has(span)") Selects all selected elements that have a descendant element matching the specified selector.
:empty $(":empty") Selects all selected elements that have no child elements.
:parent $(":parent") Selects all selected elements that have child elements.

Selecting input Elements

In jQuery, you can easily select specific elements related to input forms.

When elements exist as follows:

<div><input type="checkbox" id="espresso"><label for="espresso">Espresso</label></div>
<div><input type="checkbox" id="americano"><label for="americano">Americano</label></div>
<div><input type="checkbox" id="caffeLatte"><label for="caffeLatte">Caffe latte</label></div>

The following example gets the number of checked input form elements among checkboxes.

var checked = $(":checked");
$("#count").text("You selected " + checked.length + " kinds of coffee.");

Run code

Selectors that can select specific <input> elements are as follows.

Selector Example Description
:button $(":button") Selects all elements whose type attribute value is "button".
:checkbox $(":checkbox") Selects all elements whose type attribute value is "checkbox".
:file $(":file") Selects all elements whose type attribute value is "file".
:image $(":image") Selects all elements whose type attribute value is "image".
:password $(":password") Selects all elements whose type attribute value is "password".
:radio $(":radio") Selects all elements whose type attribute value is "radio".
:reset $(":reset") Selects all elements whose type attribute value is "reset".
:submit $(":submit") Selects all elements whose type attribute value is "submit".
:text $(":text") Selects all elements whose type attribute value is "text".
:input $(":input") Selects all <input>, <textarea>, <select>, and <button> elements.
:checked $(":checked") Selects all checked elements among elements whose type attribute value is "checkbox" or "radio".
:selected $(":selected") Selects all selected <option> elements.
:focus $(":focus") Selects the element that currently has focus.
:disabled $(":disabled") Selects all disabled elements.
:enabled $(":enabled") Selects all enabled elements.