jquery - Check if a div has all four classes -
basically filter wordpress.
all posts shown in 1 page , have added category names classes main div (.box) of post 1 div have multiple classes.
there fours drop downs in filter widget categories listed options.
i wanted show div has active category names classes.
following code. if condition, how can use idea of "and"?
this code gives me 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(); } }); });
you can use $.fn.filter method 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 string of 4 classes concatenated .
character: .class1.class2.class3.class4
, css selector .box
element having 4 classes @ same time.
Comments
Post a Comment