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?
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?
Share Improve this question edited May 9, 2013 at 10:41 user2022859 asked May 9, 2013 at 10:32 SaaSaa 637 bronze badges 1- possible duplicate of jQuery get all divs which do not have class attribute – palaѕн Commented May 9, 2013 at 10:44
6 Answers
Reset to default 10You can use :not
selector
$('div:not([class])');
here is API
And a simple Fiddle
Use :not
selector to filter
$('div:not([class])');
Combine the :not()
selector with the attribute present selector [class]
.
$("div:not([class])")
jsFiddle.
Another option is to use .not() with Has Attribute Selector
$('div').not('[class]')
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.
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"