Not class selector in jQuery

Not class selector in jQuery

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

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?

Solutions/Answers:

Solution 1:

You need the :not() selector:

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

or, alternatively, the .not() method:

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

Solution 2:

You can use the :not filter selector:

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

Or not() method:

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

More Info:

Related:  Why was block scope not originally implemented in JavaScript?