Skip to content

jQuery Selectors and Actions: What is $(selector).action() and How Is It Used?

Hello! Today, we will discuss one of the fundamental building blocks of web development, jQuery’s famous selector and action logic. You know, that system where you grab elements on a webpage and make them do things, that’s what we’ll uncover. I remember when I first started web projects, working with these selectors felt a bit like magic. You write something, see it work, then see it explode… Luckily, over time, I understood the logic, and now these tasks are much easier.

The gist is quite simple. Imagine a stage with many actors (these are your HTML elements), and you are the director. As a director, which actor will you call upon, and what will you make them do? That’s exactly what jQuery’s $(selector).action() structure does. The first part, $(selector), specifies which actor you are calling. The second part, .action(), tells the called actor what to do.

This $(selector) part is the key. Because if you don’t select the right element here, no matter what you do, the results will be disappointing. For example, if you want to select an element with a specific ID. IDs are unique, like an address for a house. So you’d use #. For example, if you have a div with the ID ‘mainDiv’, you’d write $('#mainDiv'). It’s very simple.

What about classes? Classes can be assigned to multiple elements, like a group. For example, if you want to select all buttons with the class ‘btn’, you just write $('.btn'). This is quite understandable too.

You can also select elements by their own tags. If you want to select all paragraph tags on the page? Great! Just $('p'). Or all images? $('img')! Sometimes these methods accelerate your work tremendously, instantly targeting the entire page. Of course, you need to be careful, as you might select unintended elements 🙂

Now, let’s move on to the action part. We’ve called the actor, now what will we make it do? The most commonly used ones are .hide() and .show(). As their names suggest, they hide or show the element. Also, .click() exists, which is perfect for capturing click events. Want something to happen when you click a button? That’s where .click() is your savior.

There are also .html() and .text(). These are used to change the content of an element. You can send HTML code with .html(), and text with .text(). For example, if you want to write 'Hello World!' into a div, you would use $('#messageDiv').text('Hello World!');. Isn't it incredible? You can change things in seconds.

Sometimes you'd like to know what something is without touching it. That's where .css() method comes in handy. For example, want to learn the color of an element? Use $('#someElement').css('color') and it will tell you the color. Or, want to change its color? $('#someElement').css('color', 'red'); That's how simple it is. From my experience, CSS work used to drive me crazy, but with jQuery, it became much more practical.

Now let's get into a practical example. Suppose we have a button, and every time we click it, we want the neighboring paragraph's color to turn red. Let's prepare our HTML first:

<button id="changeColorBtn">Change Color</button>

<p class="description">This paragraph's color will change.</p>

<p>This one will not change.</p>

Now, let's move to our jQuery code:

// WRONG APPROACH (Only button click, no selector)

$(document).ready(function(){

$("#changeColorBtn").click(function(){

$(".description").css("color", "red");

});

});

This code works correctly but has a shortcoming. Why? Because we've used $(document).ready(), which is good. It ensures the code runs after the entire page has loaded. But the main point is, we're selecting .description class element and turning its color red. Sometimes, in initial attempts, focusing on just one class isn't bad, right?

Now, let's look at a more comprehensive and correct approach. Maybe we want to change the color of every paragraph with the .description class, not just one. Then, our code should look like this:

$(function(){ // shorthand for $(document).ready()

$("#changeColorBtn").click(function(){

$(".description").css("color", "red");

});

});

As you can see, the $(selector).action() logic is very clear here. $("#changeColorBtn") selects the button, .click(function(){ ... }); assigns a click action. Inside this click function, $(".description") selects all paragraphs with the class .description, and .css("color", "red") turns their color red. Isn't this beautiful?

By the way, sometimes there are multiple elements with the same class and you want to target only one. In that case, you have to be more specific. For example, to select the first element with .description class, use $('.description').first().css('color', 'blue');. To select the last one, use .last(). Believe me, these small nuances make things so much easier.

The topic of selectors and actions is so broad that it's impossible to explain everything here. But to grasp the general logic, these examples should suffice. Remember, in web development, the core lies in selecting the right elements and making them do what you want. That is, this $(selector).action() structure is like a magic wand that lets you control everything on your web page.

Ultimately, jQuery's fundamental structure makes web development much faster and more fun. I have often used this structure in my projects and always found it effective. If you're just starting out, it's very important to solidify this basic logic. It might seem complicated at first, but as you practice, everything will fall into place. As they say, practice makes perfect, and that's exactly how it works here.

By the way, if you want to learn more details, you can search on Google. You'll find many examples and explanations. I learned a lot from there, especially from YouTube.

In conclusion, this $(selector).action() pattern of jQuery is the key to bringing your web page elements to life. Use the right selector, find the right element, and then give it the desired action. It's that simple!