![]() |
What is a jQuery Selector?jQuery selectors are functions that allow you to target and select HTML elements in the DOM based on element names, IDs, classes, attributes, and more, facilitating manipulation and interaction. Syntax: $(" ")
Note: jQuery selector starts with the dollar sign $, and you enclose the selector inside parentheses (). We will explore some basic selectors along with the help of examples. jQuery #id SelectorjQuery #id selector allows you to target specific elements by their unique ID attribute using the # symbol followed by the ID name. Syntax: $( "#id-name" )
Example: In this example, we added two buttons with IDs “colorButton” and “hideButton.” The first button is responsible for changing the background color of the first paragraph (with ID “p1”) to sky blue, and the second button is responsible for hiding the second paragraph (with ID “p2”). HTML
Output: jQuery .Class SelectorIn jQuery, a class selector is used to target HTML elements by their class attribute using the dot ( . ) symbol followed by the class name. Syntax: $( ".class" )
Example: In this example, we are using a class selector to target our given p tag. HTML
Output: ![]() CSS selector jQuery Tag Name SelectorIn jQuery, a tag name selector is used to target HTML elements by their tag names. Syntax: $( "p" );
Example: In this example, we are using a tag name selector to target our p tag and provide some styling, in which we provide green color to our given text and the background color is sky blue to our p tag. HTML
Output: ![]() tag name selector List of all jQuery SelectorsThe following table shows the various types of selectors available in jQuery:
|
Reffered: https://www.geeksforgeeks.org
JQuery |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |