I'm trying to filter through users by selecting multiple filters.
The thing is, I'm working with third party software which shows my user list in multiple tables. So each user does have unique ID but also each user is in several tables so I'm trying to filter through them.
So, for example, if you checkmark color Blue and item Bucket, it should display only users that have both of those classes.
I'm trying to use $(this).val() to get the name of the value of the checkbox and search it as class in each user, so if those two classes (or more) match with each user, it should show them in the showUsers results bellow.
I've setup example and my failed attempt at this: JSFiddle
HTML
<!-- FILTERS -->
<h3>Filter items</h3>
<div class="filter-list-one">
<input type="checkbox" value="Wood">Wood<br>
<input type="checkbox" value="Bucket">Bucket<br>
<input type="checkbox" value="Hammer">Hammer<br>
</div>
<h3>Filter colors</h3>
<div class="filter-list-two">
<input type="checkbox" value="Red">Red<br>
<input type="checkbox" value="Purple">Purple<br>
<input type="checkbox" value="Blue">Blue<br>
</div>
<!-- FILTERS END -->
<!-- SEARCHING THROUGH -->
<h3>Results</h3>
<div class="objects">
<p id="user1" class="user">
<span>wood</span>
<span class="bucket">bucket</span>
<span>hammer</span>
</p>
<p id="user2" class="user">
<span>wood</span>
<span class="bucket">bucket</span>
<span class="bucket">hammer</span>
</p>
<p id="user3" class="user">
<span class="bucket"n>wood</span>
<span class="bucket">bucket</span>
<span>hammer</span>
</p>
</div>
<div class="colors">
<p id="user1">
<span>red</span>
<span class="purple">purple</span>
<span>blue</span>
</p>
<p id="user2">
<span class="purple">red</span>
<span>purple</span>
<span>blue</span>
</p>
<p id="user3">
<span>red</span>
<span class="purple">purple</span>
<span class="purple">blue</span>
</p>
</div>
<div class="showUsers">
<div id="user1" class="filtered">I'm here and I have those things</div>
<div id="user2" class="filtered">I'm here and I have those things</div>
<div id="user3" class="filtered">I'm here and I have those things</div>
</div>
<!-- SEARCHING THROUGH NED -->
CSS
div {
margin-bottom: 20px;
}
h3 {
margin: 5px 0;
}
p {
margin: 0 5px;
}
.colors p,
.user,
.filtered {
display: none;
}
jQuery
$(".filter-list-one :checkbox, .filter-list-two :checkbox").change(function() {
if ( ($(".items" + $(this).val())[0]) && ($(".colors" + $(this).val())[0]) ) {
var showUser = $(this).closest(".user").attr("id");
$(".showUsers").find("#" + horaInicial).css("display, block");
}
});
($(".items" + $(this).val())[0])?.items?!