jquery - how do i Loop .next()? -
i have 3 divs same class, adding class 'selected' next div on click , removing previous class, working fine want loop it
currently going 1 --> 2 --> 3, want loop, 3-->1, please help...
html
<div id="all"> <div class="section selected">one</div> <div class="section">two</div> <div class="section">three</div> </div> <br /> <a href="javascript:;" id="button">click</a>
css
.selected{background:red}
js
$('#button').click(function(){ $('.section.selected').removeclass('selected').next('.section').addclass('selected'); });
js fiddle link : http://jsfiddle.net/madhuri2987/kk66g/2/
the simplest way check whether .next()
exists , if not "select" first.
var $next = $('.section.selected').removeclass('selected').next('.section'); if ($next.length) { $next.addclass('selected'); } else { $(".section:first").addclass('selected'); }
Comments
Post a Comment