I found this link where I got this code:
$(document).ready(function(){
//when a link in the filters div is clicked...
$('#filters a').click(function(e){
//prevent the default behaviour of the link
e.preventDefault();
//get the id of the clicked link(which is equal to classes of our content
var filter = $(this).attr('id');
//show all the list items(this is needed to get the hidden ones shown)
$('#content ul li').show();
/*using the :not attribute and the filter class in it we are selecting
only the list items that don't have that class and hide them '*/
$('#content ul li:not(.' + filter + ')').hide();
});
});
It worked fine, but I’m using this code in a select button. When I choose the option, it filters ok.
But I have more than one select – 5 in fact -, but I don’t know how to approach in this situacion.
Example:
Items: strawberry, apple, cherry, orange, banana, grape
Select 1 – all fruits, red color, green color, etc
Select 2 – all forms, rounded form, triangle form.
Select 3 – all another thing, another thing
The user can first choose the color red – First Filter.
then he could choose rounded, so apple and cherry would be the answers.
I’ve already tried to filter only the visible images, but some error ocurred when I try do bring the information back.
In the same example with the results apple and cherry, if the users select all fruits the results must be apple, cherry, orange and grape.
Some advice?
Here is an example code
Note that the filters are working in isolated way.
Fiddled:
http://jsfiddle.net/iambriansreed/turMe/
jQuery:
HTML
I create an object which I then assign all the filters to. I then filter out the DOM elements that don’t meet the criteria. This will now work for any number of filters.
On this line:
I am doing three things:
$(this).parent().attr('class')$(this).attr('id')filtersusing the key defined in 1 and the value defined in 2.