ndesign11
ndesign11

Reputation: 1779

Jquery if statement not working with data attribute

When filter button is clicked I would like it to hide all the li where the data attribute hasrelated=0

html

<li data-mostsaved="0" data-hasrelated="0" data-mostplayed="2">img</li>

JQ

$( ".has-related" ).click(function() {
        if ($("li").data('hasrelated') == '0') {
            $("li").hide();
        }
    });

Upvotes: 0

Views: 1022

Answers (3)

Linial
Linial

Reputation: 1154

JSFIDDLE DEMO

I've written this using $.each function by jQuery:

$("a_button").click(function(){
  $("li").each(function(){

      var a = $(this).data("hasrelated");
      if(a == 0){
       $(this).toggle();   
      }

  });

});

Upvotes: 0

Volune
Volune

Reputation: 4339

You can use the Attribute Equals Selector [name="value"] to check where the data attribute hasrelated=0, as in:

$('li[data-hasrelated=0]').hide();

Upvotes: 0

Semyon Vyskubov
Semyon Vyskubov

Reputation: 986

Try this:

$(".has-related").click(function() {
    $('li').each(function() {
        if ($(this).data('hasrelated') == '0') {
            $(this).hide();
        }
    });
});

Upvotes: 4

Related Questions