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?
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).
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.
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.
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'.
You need the :not()
selector:
$('div[class^="first-"]:not(.first-bar)')
or, alternatively, the .not()
method:
$('div[class^="first-"]').not('.first-bar');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With