There are 3 blocks with the item class. How every 3 seconds to add the class active to the next element?

I do it, but it does not work

 $(function() { $(document).each(function() { $(".item").removeClass("active"); $(".item.active").next().addClass("active"); }); }); 

    1 answer 1

    Try this:

     var params = { count: $(".item").length, now: 0 } function set_active() { if(params.now == params.count) { params.now = 0; } $(".item").removeClass("active"); $(".item[data-id=\"" + params.now + "\"]").addClass("active"); params.now++; } set_active(); setInterval(set_active, 3000); 
     .active { color: red; } 
     <script src="//code.jquery.com/jquery.min.js"></script> <div class="item" data-id="0">Привет</div> <div class="item" data-id="1">Привет</div> <div class="item" data-id="2">Привет</div> 

    • The cycle works, but after the last .item, everything disappears - asd
    • How to make it work on the new after completion? - asd
    • @asd now .... - ikerya
    • @asd updated the post, try it - ikerya
    • And this code can be altered under eq? with nth-child, the trouble turns out to be in the code ((Thanks in advance - asd