I have a task to assign specific classes to every other element in a list, starting from the first one.
Check out the list below:
<ul class="timeline">
<span class="topbullet"></span>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
<li></li>
<li></li>
<div class="clearfix"></div>
</ul>
The desired output is as follows:
<ul class="timeline">
<span class="topbullet"></span>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
<li class="odd"></li>
<li class="even"></li>
<div class="clearfix"></div>
</ul>
Here's the jQuery code I'm using:
$(".timeline li:nth-of-type(2n)").addClass('even');
While adding the 'even' classes is easy, I'm not sure how to add the 'odd' classes. Can anyone suggest the right selector to achieve this starting from the first list item?