Selecting Elements | jQuery Learning Center (2024)

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

1

$( "#myId" ); // Note IDs must be unique per page.

link Selecting Elements by Class Name

1

$( ".myClass" );

link Selecting Elements by Attribute

1

$( "input[name='first_name']" );

link Selecting Elements by Compound CSS Selector

1

$( "#contents ul.people li" );

link Selecting Elements with a Comma-separated List of Selectors

1

$( "div.myClass, ul.people" );

link Pseudo-Selectors

1

2

3

4

5

6

7

8

9

10

11

12

$( "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.

However, this test doesn't work with <tr> elements. In the case of <tr> jQuery does check the CSS display property, and considers an element hidden if its display property is set to none.

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:

1

2

3

4

// Doesn't work!

if ( $( "div.foo" ) ) {

...

}

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:

1

2

3

4

// 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.

1

var divs = $( "div" );

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.

1

2

3

4

5

6

// Refining selections.

$( "div.foo" ).has( "p" ); // div.foo elements that contain <p> 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 <select> elements (for <select> elements only, use the :selected selector):

1

$( "form :checked" );

The :checked pseudo-selector works when used with checkboxes, radio buttons and selects.

link :disabled

Using the :disabled pseudo-selector targets any <input> elements with the disabled attribute:

1

$( "form :disabled" );

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:

1

$( "form :enabled" );

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 <input>, <textarea>, <select>, and <button> elements:

1

$( "form :input" );

link :selected

Using the :selected pseudo-selector targets any selected items in <option> elements:

1

$( "form :selected" );

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:

  • :password
  • :reset
  • :radio
  • :text
  • :submit
  • :checkbox
  • :button
  • :image
  • :file

For all of these there are side notes about performance, so be sure to check out the API docs for more in-depth information.

Selecting Elements | jQuery Learning Center (2024)

FAQs

How to select all elements using jQuery available in a DOM? ›

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.

How to select all elements with the same class in jQuery? ›

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

How does jQuery selector work? ›

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.

How do you select all elements in DOM? ›

If you want to find all HTML elements that match a specified CSS selector (id, class names, types, attributes, values of attributes, etc), use the querySelectorAll() method. This example returns a list of all <p> elements with class="intro" .

Which selector selects all elements available in a DOM? ›

Universal selector selects all elements available in the document object model(DOM).

Which is the correct jQuery selector to select all elements? ›

All Selector (“*”)

Selects all elements.

How to select multiple elements at once in jQuery? ›

To select multiple elements of an html page using multiple elements selector, we pass the element names inside parenthesis, in double quotes, separated by commas. For example: $(“div, p, h2”) this will select all the div, p and h2 elements of a page.

How to select all elements with a class in javascript? ›

If there are multiple elements with the same class name, you can use the document. querySelectorAll() method to get a NodeList of all the elements, and then loop through them to perform operations. const elements = document.

What selector is used to select all elements? ›

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

How to select element content in jQuery? ›

jQuery :contains() Selector

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).

Which selector is faster in jQuery? ›

Class selectors are the slowest selectors in jQuery. Which is the fastest selector in jQuery ? ID and Element selector are the fastest selectors in jQuery.

How to select all elements with a class in jQuery? ›

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.

How many jQuery selectors are there? ›

There are three categories of jQuery selectors: Basic CSS selectors, Positional selectors, and Custom jQuery selectors. The Basic Selectors are known as "find selectors" as they are used to find elements within the DOM.

How to find element in DOM using jQuery? ›

Given a jQuery object that represents a set of DOM elements, the .find() method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements.

How to select all input elements in jQuery? ›

$( ":text" ) allows us to select all <input type="text"> elements. As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ( "*" ) is implied.

How to select multiple elements on DOM JavaScript? ›

Javascript provides us with the querySelectorAll() method to select multiple elements. The method will return the elements in an array. As with the other muli-selection methods, we access elements with the array indexer.

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6253

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.