سؤال

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.

هل كانت مفيدة؟

المحلول

Try to do:

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

نصائح أخرى

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();
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top