jQuery Tutorial: One Method 🎯

beginner
24 min

jQuery Tutorial: One Method 🎯

Welcome to the first lesson in our jQuery Tutorial series! In this article, we'll dive deep into one of the most essential and powerful jQuery methods - $().

What is jQuery? 📝

jQuery is a JavaScript library that simplifies HTML document traversing, event handling, and animation. It was designed to make it easier for developers to manipulate web pages, making it a popular choice for building interactive websites and applications.

The $() Method 💡

The $() method is the cornerstone of jQuery, allowing us to select HTML elements from our web pages. This method can work with various types of selectors, making it incredibly versatile.

Using $() with HTML Elements ✅

Let's start with a simple example. Suppose we have an HTML structure like this:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <h1 id="header">Hello, World!</h1> <p>This is a paragraph.</p> <script> // Select the header using $() var header = $("header"); console.log(header); </script> </body> </html>

In this example, we're using the $() method to select the header element and store it in a variable called header.

Quick Quiz
Question 1 of 1

What does the `$()` method do in the given example?

Using $() with CSS Selectors ✅

jQuery also supports CSS selectors, making it easy to target specific elements based on their attributes, class names, and more. Here's an example:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <h1>Header 1</h1> <h1 class="special-header">Header 2</h1> <h1>Header 3</h1> <script> // Select the heading with class 'special-header' using $() var specialHeader = $(".special-header"); console.log(specialHeader); </script> </body> </html>

In this example, we're using the . dot to target the heading with the class special-header.

Quick Quiz
Question 1 of 1

Which heading will be selected using the given jQuery code?

Using $() with Event Handling ✅

jQuery also simplifies event handling. Here's an example where we attach a click event to the header element:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <h1 id="header">Click me!</h1> <script> // Attach a click event to the header using $() $("#header").click(function() { alert("Header clicked!"); }); </script> </body> </html>

In this example, we're using the # hash to target the header element with the ID header. When you click the header, an alert box will appear.

Quick Quiz
Question 1 of 1

What will happen when you click the header in the given example?

Conclusion ✅

In this first lesson, we've learned about the $() method in jQuery, which allows us to select HTML elements from our web pages. We've seen examples using simple HTML elements, CSS selectors, and event handling.

In the next lessons, we'll dive deeper into jQuery and explore more powerful methods, animation, and even AJAX!

Keep learning, and happy coding! 🚀💻🎓