Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not class selector in jQuery

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?

like image 311
Zardoz Avatar asked Jan 06 '11 10:01

Zardoz


People also ask

Which is not a selector available in jQuery?

jQuery :not() SelectorThe :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).

What is not in jQuery?

The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”. Syntax: $(selector).not(A) The selector is the selected element which is not to be selected.

How do I select a class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.

Is jQuery a selector?

The is( selector ) method checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector. If no element fits, or the selector is not valid, then the response will be 'false'.


1 Answers

You need the :not() selector:

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

or, alternatively, the .not() method:

$('div[class^="first-"]').not('.first-bar'); 
like image 97
lonesomeday Avatar answered Sep 16 '22 18:09

lonesomeday