jQuery - Selector Method

jQuery is a popular JavaScript library that provides a simplified and concise way to manipulate HTML documents, handle events, and add animation effects. The jQuery selector method is used to select and manipulate HTML elements on a web page.

The jQuery selector method is based on the CSS selector syntax, which allows you to select elements based on their attributes, IDs, classes, and more. Here is the basic syntax of the jQuery selector method:

$(selector).action()

In this syntax, the $ sign is a shorthand for the jQuery function, which creates a jQuery object that wraps the selected elements. The selector parameter is a string that specifies the element(s) to select. Finally, the action parameter is a jQuery method that performs an action on the selected elements.

Here are some examples of jQuery selector methods:

Selecting elements by tag name:

$("p")  // selects all <p> elements on the page

Selecting elements by class name:

$(".my-class")  // selects all elements with class="my-class"

Selecting elements by ID:

$("#my-id")  // selects the element with id="my-id"

Selecting elements by attribute:

$("[href]")  // selects all elements with a href attribute

$("[href='#']")  // selects all elements with href="#" attribute

Selecting child elements:

$("ul li")  // selects all <li> elements that are children of <ul>

 

These are just a few examples of the many ways you can use the jQuery selector method to select and manipulate HTML elements. Once you have selected the elements you want to work with, you can then use jQuery methods to perform various actions on them, such as changing their content, modifying their attributes, and adding or removing classes.