in jQuery, how do i filter all contents that contain certain class?
HTML:
<div class='item 10'>10</div>
<div class='item 20'>20</div>
<div class='item 30'>30</div>
<div class='item 40'>40</div>
<div class='not-an-item 50'>50</div>
what i’ve tried:
var classList = $(".item").attr('class').split(/\s+/);
if(classList[1] == '10' || lassList[1] == '30'){
$(".item").hide();
}
correct output:
<div class='item 20'>20</div>
<div class='item 40'>40</div>
<div class='not-an-item 50'>50</div>
You can use the
.has()method or the.not()method to reduce the set of elements. With.has()you keep all elements that match a certain selector, while.not()keep all elements that doesn’t match the selector.For more advanced filtering, your can use the
.filter()method.If I understand your question correctly, you could use
.filter()with a filtering-function. Something like this: