Not class selector in jQuery

05/15/2020 05:00:02

Is there a simple selector expression to not select elements with a specific class?

<div class="first-foo" />
<div class="first-moo" />
<div class="first-koo" />
<div class="first-bar second-foo" />

I just want to get the first three divs and tried

$(div[class^="first-"][class!="first-bar"])

But this receives all as the last div contains more than first-bar. Is there a way to use a placeholder in such an expression? Something like that

$(div[class^="first-"][class!="first-bar*"]) // doesn't seem to work

Any other selectors that may help?

Verified Answer (538 Votes)

01/06/2011 15:53:15

You need the :not() selector:

$('div[class^="first-"]:not(.first-bar)')

or, alternatively, the .not() method:

$('div[class^="first-"]').not('.first-bar');
538

Answer #2 (85 Votes)

01/06/2011 15:53:38

You can use the :not filter selector:

$('foo:not(".someClass")')

Or not() method:

$('foo').not(".someClass")

More Info:

85
2
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.