I'd suggest:
$('div').not('[class]');
JS Fiddle demo, note that this approach fails to select the <span>
element ( pertinent HTML below).
Or, to select all elements without a class attribute:
$('body *:not("[class]")')
JS Fiddle demo, note that this also fails to select the <span>
element.
The universal selector (*
) is implied in the case of a pseudo-class selector being used without a tag-name, but in the event it's used I prefer to be explicit about it, though it is optional in this context.
Further, to select all elements, within the <body>
, with either no class
attribute, or with a class attribute, but no set class-name:
$('body *').filter(function(){
return !this.className || !this.className.length;
})
JS Fiddle demo, this approach, because it tests for both the absence of a className
property as well as an empty/zero-length className
property, successfully finds the <span>
.
The above demos were all used with the following HTML:
<div class="nope">
Some text that should not be selected.
<div>You need me!</div>
</div>
<span class="foo">Not me</span>
<span class="">Me too (probably)</span>
References: