user979331
user979331

Reputation: 11911

jQuery only toggle this element

I have this code:

$("#communities .inventory-search button.dropdown-toggle").on("click", function () {
    $(".inventory-search .dropdown-toggle img").toggle();
});

This works, but it keeps toggling all the .dropdown-toggle img items, how do I just target the one that was clicked?

Upvotes: 1

Views: 37

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

You should use $(this) instead to refer to the current clicked element like:

$("img", this).toggle();
//Or
$(this).find("img").toggle();

Else all the images inside .dropdown-toggle will be toggled on click not just the clicked one, full code will be :

$("#communities .inventory-search button.dropdown-toggle").on("click", function () {
    $("img", this).toggle();
});

Hope this helps.

Upvotes: 2

ced-b
ced-b

Reputation: 4065

You should be able to do something like:

$("#communities .inventory-search button.dropdown-toggle").on("click", function () {
   $(this).find("img").toggle();
});

Upvotes: 1

Related Questions