0

I need to filter the contents by using jquery but I need to display all contents if no checkbox is selected.

Here is my code:

     <div class="tags">
    <label>
        <input type="checkbox" rel="arts" />
        Arts
    </label>
    <label>
        <input type="checkbox" rel="computers" />
        Computers
    </label>
    <label>
        <input type="checkbox" rel="health" />
        Health
    </label>
    <label>
        <input type="checkbox" rel="video-games" />
        Video Games
    </label>
</div>
<ul class="results">
    <li class="arts computers">Result 1</li>
    <li class="video-games">Result 2</li>
    <li class="computers health video-games">Result 3</li>
    <li class="arts video-games">Result 4</li>
    <li class="arts video-games">Result 6</li>
</ul>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

<script>

$(document).ready(function () {
        $('.results > li').show();

        $('div.tags').find('input:checkbox').on('click', function () {
            $('.results > li').hide();
            $('div.tags').find('input:checked').each(function () {
                $('.results > li.' + $(this).attr('rel')).show();
            });
        });
    });      
</script>

onload it shows all contents. But if I deselect all checkboxes, all <li>'s content are hiding. Instead, I need to show all content if I deselect all checkboxes after deselection.

3 Answers 3

2

Add a checks class to all your checkboxes class="checks" then you can use an if/else conditional statement, while using .change() and .show().

I.e.: <input type="checkbox" rel="arts" class="checks" /> etc.

Note: I'm not a JS guru, but the following addition to be placed under your existing <script>...</script> will do what you want to achieve.

<script type="text/javascript">

$('.checks').change(function(){
    if(this.checked) {

    $('.checks').show();

}

else{
    $('.checks').hide();
}

});

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

okay. with this code in all times : checks have 'show()'. change it to hide() in else block.
1

change checkboxes like this:

<input type="checkbox" data-type="computers" />

and then:

$('.tag checkbox').on('change', function() {
  chckbxType = $(this).data('type');
  $('.results li').each(function() {
      $(this).hasClass(chckbxType) ? $(this).show() :  $(this).hide();
  }
}

It isn't the best solution but it works ;) for improving quality of this code you should check visibility before using show or hide...

Comments

0

On checkbox click check if any checkbox is checked or not. If any checkbox is checked then show filtered results else show all results.

<script>

$(document).ready(function () {

    $('div.tags').find('input:checkbox').on('click', function () {
        if($('div.tags input:checked').length > 0){   // check
            $('.results > li').hide();
            $('div.tags').find('input:checked').each(function () {
                $('.results > li.' + $(this).attr('rel')).show();
            });
        }
        else{
            $('.results > li').show();
        }
    });

});      
</script>

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.