0

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();
        }
    });
1
  • $("li").data is going to fetch the FIRST <li> found in the document, grab its data attribute, and test that. You need to test EVERY <li> that has a hasrelated attribute in turn. Commented Aug 22, 2014 at 21:33

3 Answers 3

4

Try this:

$(".has-related").click(function() {
    $('li').each(function() {
        if ($(this).data('hasrelated') == '0') {
            $(this).hide();
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

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();   
      }

  });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.