I have a <ul> of items
Then I have an array of classes to filter.
Now I want to loop through all <li> and show only the items that contain all of the classes in the array and hide all the others.
How can I do that?
Code so far (untested):
JS
define([
"jquery"
], function($){
"use strict";
function main(config, element) {
$('.filter-option').on('change', function (event) {
const activeFilters = $('.filter-option:checked');
if (!activeFilters) {
$('.product').forEach(function(item) {
item.show();
});
} else {
$('.product').forEach(function(item) {
//if item has all classes from activeFilters id's show else hide
});
}
});
}
return main;
});
HTML
<div class="filter-box">
<h3>Size</h3>
<input type="checkbox" class="filter-option" id="size-s" name="size-s">
<label for="size-s" class="filter-option-label">Small</label>
<input type="checkbox" class="filter-option" id="size-m" name="size-m">
<label for="size-m" class="filter-option-label">Medium</label>
<input type="checkbox" class="filter-option" id="size-l" name="size-l">
<label for="size-l" class="filter-option-label">Large</label>
<h3>Colour</h3>
<input type="checkbox" class="filter-option" id="color-white" name="color-white">
<label for="color-white" class="filter-option-label">White</label>
<input type="checkbox" class="filter-option" id="color-red" name="color-red">
<label for="color-red" class="filter-option-label">Red</label>
<input type="checkbox" class="filter-option" id="color-blue" name="color-blue">
<label for="color-blue" class="filter-option-label">Blue</label>
</div>
<ul id="product-list">
<li class="product color-red size-l">Red Large</li>
<li class="product color-red size-m">Red Medium</li>
<li class="product color-red size-s">Red Small</li>
<li class="product color-blue size-l">Blue Large</li>
<li class="product color-blue size-m">Blue Medium</li>
<li class="product color-blue size-s">Blue Small</li>
<li class="product color-white size-l">White Large</li>
<li class="product color-white size-m">White Medium</li>
<li class="product color-white size-s">White Small</li>
</ul>