-1

So i have let's say three divs

<div class="a"></div>
<div class="b"></div>
<div></div>
     .
     .
<div class="n"></div>

Is there a way to select the div that doesnt have any class? I dont know the order of the elements in DOM, so using something like .next() wouldnt work.

Beginner
  • 53
  • 7
  • Uhm, use "div"? Or third child? –  Oct 26 '16 at 08:47
  • 5
    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) – Enkode Oct 26 '16 at 08:48
  • 1
    @AllDani then you would get all divs – Pete Oct 26 '16 at 08:48
  • 2
    It's **well worth** your time to read through [the jQuery API](http://api.jquery.com) from beginning to end. It only takes an hour, maybe two at the most, and it pays you that time back right away. – T.J. Crowder Oct 26 '16 at 08:48
  • 1
    Next time, please be sure to include important details like the edit about order in the *first* version of the question. – T.J. Crowder Oct 26 '16 at 08:53
  • Yes sir. Thank you. – Beginner Oct 26 '16 at 09:10

4 Answers4

4

Try it with the :not() pseudo-class selector:

$('div:not([class])')
Arun
  • 257
  • 1
  • 5
  • 22
1

Edit: don't think the downvotes are fair when the question changes.

 $('div:not([class])')
Rauno
  • 616
  • 8
  • 22
0

$('div:not(.a, .b, .n)').addClass('red')
.red{color:red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="a">123</div>
<div class="b">123</div>
<div>123</div>

<div class="n">123</div>

You can use :not() to exclude div

guradio
  • 15,524
  • 4
  • 36
  • 57
0

You can either by css or by jquery separate the div have no class. Example shown below. Both can filter the element without class attribute. In css it select div without class using not selector. And in Jquery also method is same as like css.

<!doctype html>
<html>
 <head>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
  <style>
   div:not([class]){ color: red; }
  </style>
  <script>
   $(document).ready(function(){
    var noClassElement = $('div:not([class])');
    console.log(noClassElement)
   });
  </script>
 </head>
 
 <body>
  <div class="a">sdf df sfsdf</div>
  <div class="b">sdfsdsdsdf</div>
  <div>dfgdfg</div>
  <div class="n">sdfsdf</div>
 </body>
</html>