Html – How to add ‘odd’ and ‘even’ classes to divs (Jquery)

classhtmljquery

How to add 'odd' and 'even' classes to divs using Jquery?
I have this html

<div class="carousel" >
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
</div> 

and I need to add 'odd' and 'even' classes to the 'div.item' to get this

<div class="carousel" >
   <div class="item odd"></div>
   <div class="item even"></div>
   <div class="item odd"></div>
   <div class="item even"></div>
</div> 

Best Solution

You can use :odd and :even selector

 $('.carousel item:odd').addClass('odd');
 $('.carousel item:even').addClass('even');