Question

I have the following code:

$(".wishlist-icon").click(function() {
  $(".wishlist-icon[data-wish-name='product']").hide();
});

I wan't to change the selector inside the function to "this", but it's not working for me. How should I type it out?

So if "this" has data-wish-name='product', then hide it.

Était-ce utile?

La solution

Try to do:

$(".wishlist-icon").click(function() {
    if($(this).data('wish-name') == "product") {
        $(this).hide();
    }
});

Autres conseils

you can do in two ways:

1)

$(".wishlist-icon").click(function() {
    if($(this).attr('data-wish-name') == "product") {
        $(this).hide();
    }
});

2)

 $(".wishlist-icon").click(function() {
        if($(this).data('wish-name') == "product") {
            $(this).hide();
        }
    });

Try this:

$(".wishlist-icon").click(function() {
  $(this).find("[data-wish-name='product']").hide();
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top