Select elements without any class [duplicate]

Use :not() and the attribute not selector [att!=val] to filter out elements with a non-empty class attribute:

$('span:not([class!=""])')

jsFiddle preview

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 no class attribute, and
  • span elements that do have a class 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.

Leave a Comment