Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select elements that don't have any of several classes using jQuery?

I have a page with many elements that have the same class attached to them:

<div class="everyDiv"></div>
<div class="everyDiv"></div>
<div class="everyDiv"></div>
<div class="everyDiv"></div>
...

I add additional classes based on filters the user chooses to hide/display them:

<div class="everyDiv hide1"></div>
<div class="everyDiv hide2"></div>
<div class="everyDiv hide3"></div>
<div class="everyDiv hide2 hide3"></div>
...

Now, I need to select a range (using slice()) of the .everyDiv elements that DON'T have any of the "hide" classse - .hide1 .hide2 .hide3.

How can I do this with jQuery?

I've tried the following without success:

$("div.everyDiv").not(".hide1").not(".hide2").not(".hide3").slice(n1, n2);

$("div.everyDiv:not(.hide1):not(.hide2):not(.hide3)").slice(n1, n2);

This doesn't work either:

$("div.everyDiv:not(.hide1), div.everyDiv:not(.hide2), div.everyDiv:not(.hide3)").slice(n1, n2);

Basically, all of the "hide#" classes have CSS of display: none;, so I need to select my specified range of the divs that aren't "hidden".

like image 612
Eric Belair Avatar asked Dec 01 '22 08:12

Eric Belair


2 Answers

this should do it:

$('div.everyDiv:not(.hide1, .hide2, .hide3)').hide();

http://jsfiddle.net/s9uyk/

as per comments: making it a little more obvious what the fiddle is doing: not it adds a class to all the ones that DON'T Have any of the hide classes. http://jsfiddle.net/s9uyk/2/

like image 123
Patricia Avatar answered Jun 06 '23 15:06

Patricia


$('div.everyDiv').not(".hide1, .hide2, .hide3")

with a working Jsfiddle demo

like image 35
Phil Avatar answered Jun 06 '23 15:06

Phil