Take the 2-minute tour ×  I have 3 divs with same class, i am
adding class 'selected' to NEXT DIV on click and removing it
from previous class, its working fine but I want to loop it
Currently its going from 1 --> 2 --> 3, I want it to loop,
3-->1, please help...   <div id="all"> <div class="section
selected">ONE</div> <div class="section">TWO</div> <div
class="section">THREE</div> <br /> <a href="javascript:;"
id="button">CLICK</a>      JS Fiddle Link :
http://jsfiddle.net/madhuri2987/KK66g/2/  share|improve this
question add comment  3 Answers  up vote 3 down vote accepted
The simplest way would just be to check whether .next() exists
and if not "select" the first.  var $next =
$('.section.selected').removeClass('selected').next('.section');
if ($next.length) { else {   share|improve this answer Oh! Wow,
thanks a ton :) –  itsMe Mar 26 '13 at 3:01 Explosion Pills,
would you mind taking a look at my answer? I'm wondering what
you think. What you have above was my first thought, before you
posted it, so I m