39

I need to find, via jQuery selectors, all spans in a page that have no class.

Example:

<span class='Cool'>do not found me</span>
<span>me, me, take me please!!!</span>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
PadovaBoy
  • 453
  • 1
  • 6
  • 10

2 Answers2

64

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Hi Bolclock! Not work for me! But instead this work very well: span:not([class]) Thank you for ur suggestion!!! I made it ;) – PadovaBoy Feb 14 '11 at 11:35
  • @PadovaBoy: How can my code not work for you? `[!=""]` has been around since the first version of jQuery. – BoltClock Feb 14 '11 at 11:35
  • i don't know...it simply not work...i use Firebug with FireFinder with FF version 3.6.13 under window7 to test the selector – PadovaBoy Feb 14 '11 at 12:03
  • @PadovaBoy: In any case, if `:not([class])` works, that's fine :) – BoltClock Feb 14 '11 at 12:04
  • yep! tnx to your suggestion i found my way! This is the important thing! many many tnx! ps: in theory my way is a 'better way' because jquery should be test if the attribute is present (['class'] === undefined), in your code it test if is empty: 2 different thing! pps: it's a my personal supposition! – PadovaBoy Feb 14 '11 at 12:12
  • 3
    @brass-kazoo: The CSS version of the first selector is a little different as there isn't a `[!=]` attribute selector; you'd use `span:not([class]), span[class=""]` instead. In most cases, though, you should be able to get away with `span:not([class])`. You'll usually only find empty `class` attributes in markup generated by scripts or by people who aren't paying attention :) – BoltClock Nov 08 '11 at 04:37
  • The `span:not([class!=""])` selector mentioned above failed in IE11. Using `span:not([class]), span[class=""]` as suggested by @BoltClock gives cross browser success. – gfullam Jun 27 '14 at 15:51
  • @gfullam: I opened my fiddle in IE11 and it seems to work fine. Either way I would definitely favor valid CSS alternatives over non-standard their jQuery counterparts where possible. – BoltClock Jun 27 '14 at 15:54
  • @BoltClock I didn't do the test in isolation; I put the code straight into a working project. It only failed in IE11 for me, but there was probably some interference with other libraries, etc. – gfullam Jun 27 '14 at 19:45
4

See this answer to do it with css only Can I write a CSS selector selecting elements NOT having a certain class?

:not([class])

Actually, this will select anything witch do not have a .class applied to it.

I gathered a jsfiddle demo

html

<h2 class="fake-class">fake-class will be green</h2>
<h2 class="">empty class SHOULD be white</h2>
<h2>no class should be red</h2>
<h2 class="fake-clas2s">fake-class2 SHOULD be white</h2>
<h2 class="">empty class2 SHOULD be white</h2>
<h2>no class2 SHOULD be red</h2>

css

h2 {color:#fff}
:not([class]) {color:red;background-color:blue}
.fake-class {color:green}
Community
  • 1
  • 1
Milche Patern
  • 19,632
  • 6
  • 35
  • 52
  • OP asked for jquery selectors tho – berbt May 26 '14 at 20:25
  • 1
    @berbt .. who needs to translate a .css-selector to a $('.css-selector') ? – Milche Patern May 27 '14 at 14:50
  • 1
    Your solution is acceptable and used in the selected answer, but yes - since the author asked for a jQuery context, it would have been good to provide an answer in this context while explaining why it is also valid in css – emc Oct 17 '15 at 06:46