1

Basically this is a filter for wordpress.

All posts are shown in one page and I have added category names as classes to the main div (.box) of a post where one div will have multiple classes.

There are fours drop downs in the filter widget with all the categories listed as options.

I wanted to show only that div which has all the active category names as classes.

Following is the code. With the IF condition, how can I use idea of "AND"?

This code gives me an error.

$('#filter select').change(function()
    {
            var upper=$('#upper').val();
            var sole=$('#sole').val();
            var toe=$('#toe').val();
            var midsole=$('#midsole').val();

            $('.box').each(function()
            {

                if($(this).hasClass(upper) && hasClass(sole) && hasClass(toe) && hasClass(midsole))
                {
                    $(this).show();
                }
                else{
                    $(this).hide();
                }
    });

});
kapantzak
  • 11,610
  • 4
  • 39
  • 61
Rahul
  • 346
  • 1
  • 19

6 Answers6

4

You can use $.fn.filter method like this with combined class selected:

$('#filter select').change(function () {
    var upper = $('#upper').val();
    var sole = $('#sole').val();
    var toe = $('#toe').val();
    var midsole = $('#midsole').val();

    var classes = '.' + [upper, sole, toe, midsole].join('.');
    $('.box').hide().filter(classes).show();
});

Basically, construction '.' + [upper, sole, toe, midsole].join('.') produces a string of four classes concatenated with . character: .class1.class2.class3.class4, which is a CSS selector for .box element having all 4 classes at the same time.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • This is sick man! Worked right away. Just one more question. The first option of each dropdown list is "all" which I added manually. How do I make it work so that posts with one of the categories under that dropdown will show up if ALL is selected? Thanks a lot for the help dude. – Rahul Apr 29 '15 at 07:06
3

You forgot $(this) to hasClass.

if($(this).hasClass(upper) && $(this).hasClass(sole) && $(this).hasClass(toe) && $(this).hasClass(midsole)) {
    // Yes this div has all four classes
}
Tushar
  • 85,780
  • 21
  • 159
  • 179
0
var ele=$(this);

if(ele.hasClass(upper) && ele.hasClass(sole) && ele.hasClass(toe) && ele.hasClass(midsole))
  1. You have to give $(this) to all the .hasClass API in the if condition.
  2. So instead of repeating it assign it to a variable and use it as above.
Kawinesh S K
  • 3,148
  • 1
  • 16
  • 29
0

Your forgot the object jquery selector $(this).

    if ($(this).hasClass(upper) && $(this).hasClass(sole) && $(this).hasClass(toe) && $(this).hasClass(midsole)) {
        ...
    }
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

You can use this.hasClass("class1 class2 class3") if you sure in order, else

this.hasClass("class1) && this.hasClass("class2 ")
lails
  • 105
  • 11
0

A more beautiful way to do this: $("#myId").is(".foo.bar")

Edit: this is also independent of the order of classes

Staplerz
  • 85
  • 1
  • 10