4

I have the following html

<div class="one">One<div>
<div class="two">two<div>
<div >three<div>
<div >four<div>
<div class="three">five<div>

How would I find the div elements which don't have a class attribute? ie three and four?

Saa
  • 63
  • 7
  • possible duplicate of [jQuery get all divs which do not have class attribute](http://stackoverflow.com/questions/1962247/jquery-get-all-divs-which-do-not-have-class-attribute) – palaѕн May 09 '13 at 10:44

6 Answers6

10

You can use :not selector

$('div:not([class])');

here is API

And a simple Fiddle

dersvenhesse
  • 6,276
  • 2
  • 32
  • 53
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
4

Use :not selector to filter

$('div:not([class])');
Sarath
  • 9,030
  • 11
  • 51
  • 84
3

Combine the :not() selector with the attribute present selector [class].

$("div:not([class])")

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
2

Another option is to use .not() with Has Attribute Selector

$('div').not('[class]')
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Assuming you're not wanting to select all the dividers which have no classes, you can use nth-child to select specific ones if you know exactly where they are within a container. This will select your class-less dividers:

$('div:nth-child(3)') // Selects the third divider
$('div:nth-child(4)') // Selects the fourth divider
$('div:nth-child(3), div:nth-child(4)') // Selects both

JSFiddle example.

Alternatively you can select using .prev() and .next():

$('div.two').next() // Selects the divider after div class="two"
$('div.three').prev() // Selects the divider before div class="three"
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • But how do you find elements without a class if you _don't_ already know which ones they are? – nnnnnn May 09 '13 at 10:49
  • It really depends what you're looking for, @nnnnnn. You could use `:contains` if you're looking for specific text, for example. – James Donnelly May 09 '13 at 10:57
  • My point is that the OP is very clear on what to look for, i.e., divs that don't specify a class. Your answer doesn't cover that... – nnnnnn May 09 '13 at 20:52
0

There are different ways to do it. You could use .children() to get the list and then index into that. Or you could look up the second element and use .next() to get its sibling.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • But how do you find elements without a class if you _don't_ already know which ones they are? – nnnnnn May 09 '13 at 10:50