- jQuery Selectors
- jQuery Selectors
- jQuery Selectors
- The element Selector
- Example
- The #id Selector
- Example
- The .class Selector
- Example
- More Examples of jQuery Selectors
- Functions In a Separate File
- Selecting Elements
- link Selecting Elements by ID
- link Selecting Elements by Class Name
- link Selecting Elements by Attribute
- link Selecting Elements by Compound CSS Selector
- link Selecting Elements with a Comma-separated List of Selectors
- link Pseudo-Selectors
- link Choosing Selectors
- link Does My Selection Contain Any Elements?
- link Saving Selections
- link Refining & Filtering Selections
- link Selecting Form Elements
- link :checked
- link :disabled
- link :enabled
- link :input
- link :selected
- link Selecting by type
- Last Updated
- Suggestions, Problems, Feedback?
- Chapters
- Books
jQuery Selectors
Use our jQuery Selector Tester to demonstrate the different selectors.
Selector | Example | Selects |
---|---|---|
* | $(«*») | All elements |
#id | $(«#lastname») | The element with |
.class | $(«.intro») | All elements with > |
.class,.class | $(«.intro,.demo») | All elements with the class «intro» or «demo» |
element | $(«p») | All elements |
el1,el2,el3 | $(«h1,div,p») | All , and elements |
:first | $(«p:first») | The first element |
:last | $(«p:last») | The last element |
:even | $(«tr:even») | All even |
:odd | $(«tr:odd») | All odd |
:first-child | $(«p:first-child») | All elements that are the first child of their parent |
:first-of-type | $(«p:first-of-type») | All elements that are the first element of their parent |
:last-child | $(«p:last-child») | All elements that are the last child of their parent |
:last-of-type | $(«p:last-of-type») | All elements that are the last element of their parent |
:nth-child(n) | $(«p:nth-child(2)») | All elements that are the 2nd child of their parent |
:nth-last-child(n) | $(«p:nth-last-child(2)») | All elements that are the 2nd child of their parent, counting from the last child |
:nth-of-type(n) | $(«p:nth-of-type(2)») | All elements that are the 2nd element of their parent |
:nth-last-of-type(n) | $(«p:nth-last-of-type(2)») | All elements that are the 2nd element of their parent, counting from the last child |
:only-child | $(«p:only-child») | All elements that are the only child of their parent |
:only-of-type | $(«p:only-of-type») | All elements that are the only child, of its type, of their parent |
parent > child | $(«div > p») | All elements that are a direct child of a element |
parent descendant | $(«div p») | All elements that are descendants of a element |
element + next | $(«div + p») | The element that are next to each elements |
element ~ siblings | $(«div ~ p») | All elements that appear after the 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 , . |
: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 elements that have a element |
:empty | $(«:empty») | All elements that are empty |
:parent | $(«:parent») | All elements that are a parent of another element |
:hidden | $(«p:hidden») | All hidden elements |
:visible | $(«table:visible») | All visible tables |
:root | $(«:root») | The document’s root element |
:lang(language) | $(«p:lang(de)») | All 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 |
jQuery Selectors
jQuery selectors are one of the most important parts of the jQuery library.
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s).
jQuery selectors are used to «find» (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It’s based on the existing CSS Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
The element Selector
The jQuery element selector selects elements based on the element name.
You can select all
elements on a page like this:
When a user clicks on a button, all
elements will be hidden:
Example
The #id Selector
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.
An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
To find an element with a specific id, write a hash character, followed by the id of the HTML element:
When a user clicks on a button, the element with will be hidden:
Example
The .class Selector
The jQuery .class selector finds elements with a specific class.
To find elements with a specific class, write a period character, followed by the name of the class:
When a user clicks on a button, the elements with will be hidden:
Example
More Examples of jQuery Selectors
Use our jQuery Selector Tester to demonstrate the different selectors.
For a complete reference of all the jQuery selectors, please go to our jQuery Selectors Reference.
Functions In a Separate File
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQuery functions in a separate .js file.
When we demonstrate jQuery in this tutorial, the functions are added directly into the section. However, sometimes it is preferable to place them in a separate file, like this (use the src attribute to refer to the .js file):
Selecting Elements
The most basic concept of jQuery is to «select some elements and do something with them.» jQuery supports most CSS3 selectors, as well as some non-standard selectors. For a complete selector reference, visit the Selectors documentation on api.jquery.com.
link Selecting Elements by ID
$( "#myId" ); // Note IDs must be unique per page.
link Selecting Elements by Class Name
link Selecting Elements by Attribute
link Selecting Elements by Compound CSS Selector
link Selecting Elements with a Comma-separated List of Selectors
link Pseudo-Selectors
$( "a.external:first" );
$( "tr:odd" );
// Select all input-like elements in a form (more on this below).
$( "#myForm :input" );
$( "div:visible" );
// All except the first three divs.
$( "div:gt(2)" );
// All currently animated divs.
$( "div:animated" );
Note: When using the :visible and :hidden pseudo-selectors, jQuery tests the actual visibility of the element, not its CSS visibility or display properties. jQuery looks to see if the element’s physical height and width on the page are both greater than zero.
Elements that have not been added to the DOM will always be considered hidden, even if the CSS that would affect them would render them visible. See the Manipulating Elements section to learn how to create and add elements to the DOM.
link Choosing Selectors
Choosing good selectors is one way to improve JavaScript’s performance. Too much specificity can be a bad thing. A selector such as #myTable thead tr th.special is overkill if a selector such as #myTable th.special will get the job done.
link Does My Selection Contain Any Elements?
Once you’ve made a selection, you’ll often want to know whether you have anything to work with. A common mistake is to use:
This won’t work. When a selection is made using $() , an object is always returned, and objects always evaluate to true . Even if the selection doesn’t contain any elements, the code inside the if statement will still run.
The best way to determine if there are any elements is to test the selection’s .length property, which tells you how many elements were selected. If the answer is 0, the .length property will evaluate to false when used as a boolean value:
// Testing whether a selection contains elements.
if ( $( "div.foo" ).length )
.
>
link Saving Selections
jQuery doesn't cache elements for you. If you've made a selection that you might need to make again, you should save the selection in a variable rather than making the selection repeatedly.
Once the selection is stored in a variable, you can call jQuery methods on the variable just like you would have called them on the original selection.
A selection only fetches the elements that are on the page at the time the selection is made. If elements are added to the page later, you'll have to repeat the selection or otherwise add them to the selection stored in the variable. Stored selections don't magically update when the DOM changes.
link Refining & Filtering Selections
Sometimes the selection contains more than what you're after. jQuery offers several methods for refining and filtering selections.
// Refining selections.
$( "div.foo" ).has( "p" ); // div.foo elements that contain
tags
$( "h1" ).not( ".bar" ); // h1 elements that don't have a class of bar
$( "ul li" ).filter( ".current" ); // unordered list items with class of current
$( "ul li" ).first(); // just the first unordered list item
$( "ul li" ).eq( 5 ); // the sixth
link Selecting Form Elements
jQuery offers several pseudo-selectors that help find elements in forms. These are especially helpful because it can be difficult to distinguish between form elements based on their state or type using standard CSS selectors.
link :checked
Not to be confused with :checkbox, :checked targets checked checkboxes, but keep in mind that this selector works also for checked radio buttons, and elements (for elements only, use the :selected selector):
The :checked pseudo-selector works when used with checkboxes, radio buttons and selects.
link :disabled
Using the :disabled pseudo-selector targets any elements with the disabled attribute:
In order to get the best performance using :disabled , first select elements with a standard jQuery selector, then use .filter( ":disabled" ) , or precede the pseudo-selector with a tag name or some other selector.
link :enabled
Basically the inverse of the :disabled pseudo-selector, the :enabled pseudo-selector targets any elements that do not have a disabled attribute:
In order to get the best performance using :enabled , first select elements with a standard jQuery selector, then use .filter( ":enabled" ) , or precede the pseudo-selector with a tag name or some other selector.
link :input
Using the :input selector selects all , , , and elements:
link :selected
Using the :selected pseudo-selector targets any selected items in elements:
In order to get the best performance using :selected , first select elements with a standard jQuery selector, then use .filter( ":selected" ) , or precede the pseudo-selector with a tag name or some other selector.
link Selecting by type
jQuery provides pseudo selectors to select form-specific elements according to their type:
For all of these there are side notes about performance, so be sure to check out the API docs for more in-depth information.
Last Updated
Suggestions, Problems, Feedback?
Chapters
Books
Copyright 2023 OpenJS Foundation and jQuery contributors. All rights reserved. See jQuery License for more information. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation, please see our Trademark Policy and Trademark List. Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. OpenJS Foundation Terms of Use, Privacy, and Cookie Policies also apply. Web hosting by Digital Ocean | CDN by StackPath