Use :not()
and the attribute not selector [att!=val]
to filter out elements with a non-empty class
attribute:
$('span:not([class!=""])')
Note however that [att!=val]
is a non-standard jQuery selector, which means the selector cannot be used in CSS or with document.querySelectorAll()
. If you’re like me, and you’re a stickler for following the standards and so want to eschew non-standard jQuery selectors where possible, the following is a direct equivalent:
$('span:not([class]), span[class=""]')
This matches
span
elements that have noclass
attribute, andspan
elements that do have aclass
attribute, but only when the attribute value is empty.
In most cases, though, you should be able to get away with just the first portion:
$('span:not([class])')
You’ll usually only find empty class
attributes in markup generated by the application that’s responsible for outputting it, or by developers who aren’t paying attention.