-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
  • 6

4 Answers4

3

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

$('div:not([class])')
Arun
  • 247
  • 1
  • 4
  • 22
0

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

 $('div:not([class])')
Rauno
  • 630
  • 9
  • 21
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,359
  • 3
  • 33
  • 52
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>