jQuery Selectors 🎯

beginner
6 min

jQuery Selectors 🎯

Welcome to our deep dive into jQuery Selectors! This tutorial is designed to help you grasp the essentials of jQuery Selectors, making your web development journey smoother. Let's get started!

What are jQuery Selectors? 📝

In jQuery, Selectors are used to select HTML elements based on specific criteria. They help in finding, manipulating, and changing HTML elements easily. Understanding jQuery Selectors is crucial for anyone aiming to work with jQuery effectively.

Basic jQuery Selectors 💡

Let's dive into the world of basic jQuery Selectors.

#id Selector

The #id selector allows you to select an HTML element based on its id attribute.

html
<div id="myDiv">This is a div</div> <script> $(document).ready(function() { // Select and change the content of the div with id 'myDiv' $('#myDiv').text('Hello, World!'); }); </script>

tag Name Selector

The tagName selector allows you to select all elements of a specific tag name.

html
<p>This is a paragraph.</p> <h1>This is a heading.</h1> <script> $(document).ready(function() { // Select all paragraphs $('p').text('Changed paragraph!'); }); </script>

Class Selector

The .class selector allows you to select all elements with a specific class.

html
<div class="myClass">This is a div</div> <p class="myClass">This is a paragraph</p> <script> $(document).ready(function() { // Select all elements with class 'myClass' $('.myClass').text('Changed elements with class myClass!'); }); </script>

Combining Selectors

You can combine selectors using commas to select multiple elements at once.

html
<div id="myDiv">This is a div</div> <p>This is a paragraph.</p> <h1 class="myClass">This is a heading.</h1> <script> $(document).ready(function() { // Select the div with id 'myDiv', the paragraph, and the heading with class 'myClass' $('div#myDiv, p, .myClass').text('Selected elements!'); }); </script>
Quick Quiz
Question 1 of 1

Which jQuery Selector is used to select an HTML element based on its `id` attribute?

To be continued... Stay tuned for more on jQuery Selectors! 🚀