lundi 20 août 2018

Advanced multiple checkbox filter with jquery

I want to filter my content using checkboxes and I got some references from 'StackOverflow'. The following is the code

$(document).ready(function() {
  $('.post').show();

  $('.menu-item').find('input:checkbox').on('click', function() {
    var $posts = $('.post').hide();
    var $elements = $('.menu-item').find('input:checked');

    $posts.filter(function() {
        var $post = $(this);
        return $elements.toArray().every(function(element) {
          return $post.hasClass($(element).attr('id'));
        });
      })
      .show();
  });
});

https://jsfiddle.net/kichupreeji/rcyt19g8/12/

But my problem is, when I check doctor from checkbox it displays all doctor values and when I select Kannur and doctor it select doctor from Kannur, but when I select Kochi, Kannur, and doctor, it displays nothing. I want is when I select so, I need to display doctor from Kannur and Kochi. I knew that it could be done using by combining array.every and array.some, but I don't know about these functions. Can anyone help me




Aucun commentaire:

Enregistrer un commentaire