Im looking for a simple way to filter content based on attribue value(s). I have multiple attributes and I use checkboxes input to filter content. My problem is that I don't see how to make sure to validate one attribute before the second one.
What I mean is that one of my attribute would be a location, and my second would be an action tag. If one of the location checkboxes is disabled, I don't want any of the action tag to override the location. I don't see how to dissociate the checking conditions so if (in my example below) "paris" is disabled, unchecking or checking "have a coffee" would make the Result 1 appear.
here is what I have right now:
<div id="filtersContainer" class="tags">
<div id="location" class="filcont"><h2>Mood</h2></div>
<div id="action" class="filcont"><h2>Action</h2></div>
</div>
</div>
<div class="results">
<div class="mark" location="paris" action="have-a-coffee eat">Result 1</div>
<div class="mark" location="lyon" action="drink have-a-coffee eat">Result 2</div>
<div class="mark" location="paris lyon" action="take-away drink eat">Result 3</div>
<div class="mark" location="grenoble paris" action="drink">Result 4</div>
</div>
$( document ).ready(function() {
$("#filtersContainer").css('top', '-1000px')
//Building up filter options based on HTML data
dynamicFiltering();
//Activating all filters
$('.mark').show();
//On filter clicks
$('.tags').find('input:checkbox').on('click', function () {
$('.mark').hide();
$('.tags').find('input:checked').each(function () {
$('.mark[location~="'+$(this).attr('rel')+'"]').show();
});
});
$('.tags').find('input:checkbox').on('click', function () {
$('.mark').hide();
$('.tags').find('input:checked').each(function () {
$('.mark[action~="'+$(this).attr('rel')+'"]').show();
});
});
});
Input elements are generated dynamically based on database content. it look like this though:
<p class="check-tooltip">'+locationFilters[k]+'</p>'+
'<div class="checkboxing"><input type="checkbox" id="'+locationFilters[k]+'" checked rel="'+locationFilters[k]+'" />'+
'<label for="'+locationFilters[k]+'"></label></div>'
UPDATED SOLUTION WORKING
$( document ).ready(function() {
$("#filtersContainer").css('top', '-1000px')
//Building up filter options based on HTML data
dynamicFiltering();
//Activating all filters
$('.mark').show();
$('.tags').find('input:checkbox').on('click', function () {
var found = $.inArray($(this).attr('rel'), filterAre) > -1;
if(!found){
filterAre.push($(this).attr('rel'));
}
else{
removeA(filterAre, $(this).attr('rel'));
}
// console.log(filterAre);
filter();
});
});
function filter(){
// $(".mark").show();
$.each(filterAre, function(index, value){
$(".mark").each(function(){
var rel = $(this).attr('filter-data');
var array = rel.split(" ");
var count = 0;
for(var g=0; g<array.length; g++){
var found = $.inArray(array[g], filterAre) > -1;
if(!found){
$(this).hide();
break;
}
else{
$(this).show();
}
}
});
});
}
function removeA(arr){
var what, a= arguments, L= a.length, ax;
while(L> 1 && arr.length){
what= a[--L];
while((ax= arr.indexOf(what))!= -1){
arr.splice(ax, 1);
}
}
return arr;
}
inputelements in your elements that have classtags