I would like to ask why the following code cannot go through 1,2,3,1,2,3 in order ( jsfiddle )
while I keep clicking “next”.
When I open and run the following code in Chrome, it does not run the code in the order of "1,2,3" (it stops at 3).
HTML
<div id="slides">
<ul class="options-list">
<li><input type="radio" class="getradiotomove" id="bundle-73" name="bundle_option"checked="checked" value="73"></li>
<li><input type="radio" class="getradiotomove" id="bundle-72" name="bundle_option" value="72"></li>
<li><input type="radio" class="getradiotomove" id="bundle-74" name="bundle_option" value="74"></li>
</div>
<div id="buttons">
<a href="#" id="prev">prev</a>
<a href="#" id="next">next</a>
<div class="clear"></div>
</div>
Js
$('#next').click(function() {
var $all = $('.getradiotomove');
var $current = $('.getradiotomove:checked');
var index = $all.index($current) + 1;
if(index >= $all.length)
index = 0;
$($all[index]).attr('checked', 'checked');
return false;
});
source
share