How can I use jQuery to select an element only if it doesn't have any classes?
I'm writing a page which allows the html author to override the default jQuery action by adding a class to the element. It should be any class at all.
So the markup might be:
<ul>
<li class="override"></li>
<li></li>
</ul>
I'd like jQuery to only select the second list element because it doesn't have a class.
I've looked at .hasClass(), but it seems to require a specific class name.
Thanks!
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.
var noSpace= $('*:not([id])*:not([class])'); // 84 ?
Given a list of elements and the task is to not select a particular class using JQuery. jQuery :not() Selector: This selector selects all elements except the specified element. Parameters: It contains single parameter selector which is required.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
:not() Selector – jQuery API
$('li:not([class])')
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