Selector in jQuery
The jQuery library exploits the power of CSS (Cascading Style Sheets) Selector to help us quickly and easily access elements or groups of elements in the DOM.
The jQuery library exploits the power of CSS (Cascading Style Sheets) Selector to help us quickly and easily access elements or groups of elements in the DOM.
A jQuery Selector is a function that uses Expressions to find the matching of elements from a DOM on a given standard basis. In a simple way, you can say that the Selector is used to select one or more HTML elements by using jQuery. When an element is selected, we can perform various operations on that selected element.
The $ () base function in jQuery
Factory Function can translate as base function because for me, it is where everything is created. jQuery Selector starts with a dollar sign and parentheses $ () . The base function $ () uses three blocks while selecting elements in a given document:
- Tag Name : Shows a card name available in the DOM. For example: $ ('p') selects all paragraphs in the element.
- Tag ID ( Tag ID ): Represents a tag name available with the given ID in the DOM. The example $ ('# some-id') selects all single elements in the document that have an ID of some-id.
- Class Tag (class of tag): Represents a tag available with the given class in the DOM. For example $ ('. Some-class') selects all elements in the document that have a class that is some-class.
All of the above can be used on itself or in conjunction with another Selector. All jQuery Selector builds on the same rules except some tweaks.
Note - The $ () base function is synonymous with a jQuery function () . So in case you are using any other JavaScript library, there may be conflicts here, then you can change the $ symbol to jQuery and you can use the jQuery () function instead of the function. $ () .
For example
Here is a simple example using Tag Selecor. It will select all elements with the tag name p and will set the background color to "yellow".
The jQuery Example