title: Advanced Filtering with Javascript
body: <p>I am trying to create an advanced filter with javascript. Where I am running into issues is, when I select more than one check box, I want to show results matching items tagged with all selected checkboxes, not just ones tagged with one of the selected checkboxes. I want the search to become narrowed down as you select more checkboxes.</p>
  
  <p>For example, my website is about damage caused by wildlife. If you select tree damage and flooding, the only thing that should show up is beavers. However, right now you get anything tagged with flooding or tree damage.</p>
  
  <p>Can someone help me set up the "and" filtering rather than the "or" filtering on this? Also, is there a way to make a button clear all search criteria? </p>
  
  <p>Here is a link to my codepen: <a href="http://codepen.io/aahmed2/pen/xEBJkL?editors=0010" rel="nofollow">http://codepen.io/aahmed2/pen/xEBJkL?editors=0010</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var $filterCheckboxes = $('input[type="checkbox"]');
  var filterFunc = function() {
  
    var selectedFilters = {};
  
    $filterCheckboxes.filter(':checked').each(function() {
  
      if (!selectedFilters.hasOwnProperty(this.name)) {
        selectedFilters[this.name] = [];
      }
  
      selectedFilters[this.name].push(this.value);
    });
  
    var $filteredResults = $('.animal');
  
    $.each(selectedFilters, function(name, filterValues) {
      $filteredResults = $filteredResults.filter(function() {
  
        var matched = false,
          currentFilterValues = $(this).data('category').split(' ');
  
        $.each(currentFilterValues, function(_, currentFilterValue) {
  
          if ($.inArray(currentFilterValue, filterValues) != -1) {
            matched = true;
            return false;
          }
        });
        return matched;
  
      });
    });
  
    $('.animal').hide().filter($filteredResults).show();
  }
  
  $filterCheckboxes.on('change', filterFunc);</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    font-family: 'Arial';
    color: #646464;
  }
  .animals {
    margin-top: 30px;
  }
  .animal {
    padding: 15px 20px;
    width: 100%;
    font-weight: 700;
    background: rgba(0, 0, 0, 0.1);
    margin-bottom: 5px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div class="container"&gt;
    &lt;h1&gt;Wildlife Damage&lt;/h1&gt;
    &lt;div class="row"&gt;
      &lt;div class="col-sm-2"&gt;
        &lt;div class="btn btn-default"&gt;Clear Filters&lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="col-sm-2"&gt;
        &lt;p&gt;&lt;strong&gt;Filter Type of Damage:&lt;/strong&gt;
          &lt;br&gt;&lt;small&gt;(Select All That Apply)&lt;/small&gt;
        &lt;/p&gt;
      &lt;/div&gt;
      &lt;form&gt;
        &lt;div class="col-sm-2"&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="vehicle-damage" id="vehicle-damage" /&gt;Vehicle Damage&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="land-damage" id="land-damage" /&gt;Land Damage&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="tree-damage" id="tree-damage" /&gt;Tree Damage&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="plant-damage" id="plant-damage" /&gt;Plant Damage&lt;/label&gt;
          &lt;br&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-2"&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="structural-invasion" id="structural-invasion" /&gt;Structural Invasions&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="flooding" id="flooding" /&gt;Flooding&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="electrical-damage" id="electrical-damage" /&gt;Electrical Damage&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="nests" id="nests" /&gt;Nests&lt;/label&gt;
          &lt;br&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-2"&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="holes" id="holes" /&gt;Holes&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="holes-w-mounds" id="holes-w-mounds" /&gt;Holes with Mounds&lt;/label&gt;
          &lt;br&gt;
          &lt;label&gt;
            &lt;input type="checkbox" value="bird-egg-loss" id="bird-egg-loss" /&gt;Bird and Egg Loss&lt;/label&gt;
          &lt;br&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  
    &lt;div class="animals"&gt;
      &lt;div class="animal" data-id="aloe" data-category="structural-invasion"&gt;Bats&lt;/div&gt;
      &lt;div class="animal" data-category="flooding tree-damage"&gt;Beaver&lt;/div&gt;
      &lt;div class="animal" data-category="tree-damage plant-damage vehicle-damage"&gt;Deer&lt;/div&gt;
      &lt;div class="animal" data-category="bird-egg-loss"&gt;Feral Cats&lt;/div&gt;
      &lt;div class="animal" data-category="structural-invasion"&gt;Mice&lt;/div&gt;
      &lt;div class="animal" data-category="holes-w-mounds"&gt;Moles&lt;/div&gt;
      &lt;div class="animal" data-category="structural-invasion land-damage plant-damage bird-egg-loss"&gt;Opossum&lt;/div&gt;
      &lt;div class="animal" data-category="holes-w-mounds land-damage"&gt;Pocket Gopher&lt;/div&gt;
      &lt;div class="animal" data-category="plant-damage land-damage holes-w-mounds"&gt;Prairie Dogs&lt;/div&gt;
      &lt;div class="animal" data-category="plant-damage nests"&gt;Rabbits&lt;/div&gt;
      &lt;div class="animal" data-category="structural-invasion tree-damage land-damage plant-damage bird-egg-loss"&gt;Raccoons&lt;/div&gt;
      &lt;div class="animal" data-category="structural-invasion land-damage"&gt;Rats&lt;/div&gt;
      &lt;div class="animal" data-category="land-damage bird-egg-loss"&gt;Skunks&lt;/div&gt;
      &lt;div class="animal" data-category="structural-invasion"&gt;Garter Snakes&lt;/div&gt;
      &lt;div class="animal" data-category="vehicle-damage electrical-damage structural-invasion tree-damage"&gt;Squirrels&lt;/div&gt;
      &lt;div class="animal" data-category="holes"&gt;13-lined Ground Squirrel&lt;/div&gt;
      &lt;div class="animal" data-category="plant-damage land-damage"&gt;Voles&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>