0

I want to filter (show/hide) elements by class. I want to have lets say 5 filters working together.

<select id='filter4'>...</select>
<select id='filter5'>...</select>
<select id='filter6'>...</select>
<select id='filter7'>...</select>

and

<select id='filter6' multiple>...</select>

jquery:

jQuery(document).ready(function ($) {
    $("select").on("change", function () {
        var filterVal = $("select#filter4").val();
        var filterVal2 = $("select#filter5").val();
        var filterVal3 = $("select#filter6").val();
        var filterVal4 = $("select#filter7").val();
        var filterVal5 = $("select#filter8").val();
        var filterSelector = "";
        var filter2Selector = "";
        var filter3Selector = "";
        var filter4Selector = "";
        var filter5Selector = "";

        if (filterVal == "all" && filterVal2 == "all" && filterVal3 == "all" && filterVal4 == "all" && filterVal5 === null) {
            //show all items 
            $(".item").fadeIn("fast");
        } else {
            if (filterVal != "all") {
                filterSelector = "." + filterVal;
            }

            if (filterVal2 != "all") {
                filter2Selector = "." + filterVal2;
            }

            if (filterVal3 != "all") {
                filter3Selector = "." + filterVal3;
            }

            if (filterVal4 != "all") {
                filter4Selector = "." + filterVal4;
            }

            if (filterVal5 !== null) {
                filter5Selector = "." + filterVal5;
            }

            $(".item").hide();
            $(filterSelector + filter2Selector + filter3Selector + filter4Selector + filter5Selector).fadeIn("fast");
        }

    });
});

See everything in this jsfiddle !

The first four single filters work with any problem. The fifth multi-select element does not work (filter) properly when more than one option is selected.

EDIT: It was needed to replace commas with dots (multiple css selectors) with help of g modifier (/ /g) which is global match:

$(filter4Selector + filter5Selector.replace(/,/g, ".")).fadeIn("fast");

see http://jsfiddle.net/mahish/dum7n/ .

Answer below offers different code which works as well!

2 Answers 2

2

Cleaned up your code and fixed the multiple queries. Note that:

.class1.class2.class3 means class1 && class2 && class 3

.class1, .class2, .class3 means class1 || class2 || class 3

That was cause of earlier confusion in the comments.

jQuery(document).ready(function ($) {

    var values = [7, 8];

    $("select").on("change", function () {

        var showAll = true,
            show = [],
            joined;

        $.each(values, function (index, id) {
            var $el = $('#filter' + id),
                multi = $el.attr('multiple'),
                val = $el.val();

            if (multi) {
                if (val !== null) {
                    showAll = false;
                    $.each(val, function (i, v) {
                        show.push( v );
                    });
                }
            } else {
                if (val != 'all') {
                    showAll = false;
                    show.push( val );
                }
            }


        });

        console.log(showAll);
        console.log(show);

        if (showAll) {
            //show all items 
            $(".item").fadeIn("fast");
        } else {

            joined = '.' + show.join('.');

            console.log( joined );

            $(".item").hide();
            $( joined ).fadeIn("fast");
        }

    });

    $.each(values, function (index, id) {
        $('#filter' + id).chosen();
    });


});

http://jsfiddle.net/9SZkr/1/

19
  • Wow, thats pretty neat. Nice learning curve for me.. Although, multiple options still not working. I should change the language sry.
    – mahish
    Commented Aug 31, 2013 at 1:47
  • I wasn't adding the multiple select values properly
    – Dave Stein
    Commented Aug 31, 2013 at 1:54
  • Actually, the whole filter thing doesnt work at all right now:( Copy html from this fiddle to has letters instead of czech words to see results clearly. jsfiddle.net/mahish/Bd8S9
    – mahish
    Commented Aug 31, 2013 at 2:01
  • Keep your console open and you'll see all the classes that it's supposed to show. Your markup has a lot of em so another challenge for me :)
    – Dave Stein
    Commented Aug 31, 2013 at 2:05
  • You just need the $.each in the multi value getting.. 14 has it and so does the other
    – Dave Stein
    Commented Aug 31, 2013 at 2:06
0

Separate the selectors with commas and that should work (google jQuery multiple selectors for more information)

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.