jQuery Selectors


JavaScript is most commonly used to retrieve or change the content or value of HTML elements on a website, as well as to apply effects such as show, hide, animation, etc. However, before you can take any action, you must first locate or select the target HTML element.

Selecting items using a traditional JavaScript technique can be tedious, however here jQuery works like a charm. One of the most important features of jQuery is its ability to make DOM element selection simple and easy. In the sections to come, you'll look at some of the most popular ways to select components on a page and do something with them using jQuery.


Select elements by ID

You can use the ID picker to select a single element with a unique ID on the page. When the document is ready to be edited, the following jQuery code will select and highlight the element with the ID property ID. The code for this event will not run until the page's DOM is ready.

Example

Preview

Select Elements by Class Name

The class selector may be used to pick components that belong to a given class. When the content is complete, the following jQuery code will pick and highlight the components with the class property class="mark."

Example

Preview

Select Elements by Name

Elements may be selected using the element picker based on their names. When the document is ready, the following jQuery code will pick and highlight all of the paragraphs, i.e. the elements of the page.

Example

Preview

Select Elements by Attribute

You may use the attribute selector to choose an element based on one of its HTML characteristics, such as the target attribute of a link or the type attribute of an input, for example. When the document is complete, the following jQuery code will pick and highlight all text inputs, i.e. elements with the type="text" attribute.

Example

Preview

Select Elements by Compound CSS Selector

You may also combine CSS selectors to narrow down your options even more. For example, you may use the class selector in conjunction with an element selector to discover items in a page that are of a specific type and class.

Example

Preview

jQuery Custom Selector

jQuery adds its own custom selector in addition to the CSS specified selectors to further enhance the possibilities of choosing components on a page.

Example

Preview

FAQs

jQuery selectors are powerful tools that allow you to select and manipulate HTML elements on a web page. They follow a syntax similar to CSS selectors, enabling you to target elements based on their tag name, class, ID, attributes, and more. jQuery selectors utilize the $() function, also known as the jQuery function, to select elements and return a jQuery object. Once you have selected the desired elements, you can apply various jQuery methods and functions to manipulate their properties, contents, or perform actions. jQuery selectors provide a convenient and concise way to interact with and modify elements in the DOM.

jQuery offers a wide range of selectors to target specific elements on a web page. Some common types of jQuery selectors include:

  • Tag Selector: Selects elements based on their HTML tag name. For example, $('div') selects all <div> elements.
  • Class Selector: Selects elements based on their class attribute. For example, $('.my-class') selects all elements with the class "my-class".
  • ID Selector: Selects an element based on its ID attribute. For example, $('#my-id') selects the element with the ID "my-id".
  • Attribute Selector: Selects elements based on their attributes and attribute values. For example, $('input[type="text"]') selects all <input> elements with the type attribute set to "text".
  • Child and Descendant Selectors: Selects elements based on their hierarchical relationship. For example, $('ul li') selects all <li> elements that are descendants of <ul> elements.

These are just a few examples of the many selectors available in jQuery. jQuery selectors provide flexibility in targeting specific elements, making it easier to apply changes or perform actions on the selected elements.

Yes, jQuery selectors can be combined or chained together to create more specific and targeted selections. This is achieved by using multiple selector methods in a single jQuery statement. For example, you can combine the class selector and the tag selector by writing $('div.my-class'), which selects all <div> elements with the class "my-class". Similarly, you can chain methods to refine your selection further. For example, $('ul').find('li.my-class') selects all <li> elements with the class "my-class" that are descendants of <ul> elements. By combining and chaining selectors, you can precisely select elements based on multiple criteria, enabling you to perform more specific manipulations and actions on the selected elements.