designerNProgrammer
designerNProgrammer

Reputation: 2701

looping through LI's using Each function

Hey guys i am quite a newbie to JQuery. i have this dilemma, i am unable to loop throguh each Li's using each. Actually i am making a slider and i have these thumbs i li. Now what i've one is ive coded that on each LI click the action happens. Now i wanna make the slider automatic by click on thumbs through loop with time. here is my thumb code

  <ul id="thumbsList">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>

Now here is my jquery each function

    $( "# li" ).each(function() {
  $( this ).click();
});

Now it runs ok but runs only once. How can i loop this with a time constrainet say after every one second next li click. thanks.

Upvotes: 0

Views: 66

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try something like

var $lis = $('#thumbsList li'),
    index = 0;
setInterval(function () {
    $lis.eq(index).click();
    index = ++index % $lis.length;
}, 1000)

Demo: Fiddle

Upvotes: 1

Related Questions