0

I have a set of elements like this

<div class="wrap">
    <div class="block active">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
</div>

I need a function to infinitely loop on these elements (block) moving the active class from one to the next. I've looked at using .each() for this but I don't really understand how to use it. Any help is greatly appreciated.

5
  • you want to add some class to these DOM elements Right ? Commented Aug 5, 2016 at 11:08
  • infinitely loop when? Commented Aug 5, 2016 at 11:08
  • And when should this action happen? a button click or something? Commented Aug 5, 2016 at 11:08
  • 1
    so you want a slider, is is that hard to write "slider" Commented Aug 5, 2016 at 11:10
  • as suggested by @madalin slider is a good option, you can also run a setInterval and in the callback method find the next sibling and toggle the class on both current active block and its sibling Commented Aug 5, 2016 at 11:16

2 Answers 2

1

I think you want to do it in an interval, this is pretty easy in your case:

setInterval(function() {
    var index = $(".block.active").removeClass("active").index();
        index = index + 1 >= $(".block").length ? 0 : ++index;

    $(".block:eq(" + index + ")").addClass("active");
}, 500);
div.active { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <div class="block active">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

You can use ++index% blockLength instead.
0

You do not have to loop over all elements. You just have to do following:

  1. Fetch element with active class.
  2. Check for existence of next/previous element based on action.
  3. If exists, remove active class from current element and assign it to corresponding element.

Carousel

Sample Fiddle

$("#btnPrev").on("click", function() {
  var els = $(".block.active");
  if (els.prev() && els.prev().hasClass('block')) {
    els.removeClass("active").prev().addClass("active");
  }
})

$("#btnNext").on("click", function() {
  var els = $(".block.active");
  if (els.next() && els.next().hasClass('block')) {
    els.removeClass("active").next().addClass("active");
  }
})
.active {
  background: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="wrap">
  <div class="block active">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
  <div class="block">
    <p>content</p>
  </div>
</div>

<button id="btnPrev">Prev</button>
<button id="btnNext">Next</button>

Ticker

As commented by @atul, if you are looking for something like a news ticker, which goes on and on, you can try something like this:

Sample Fiddle

function nextTicker(){
	var els = $(".block.active");
  els.removeClass("active");
  if(els.next() && els.next().hasClass('block')){
  	els.next().addClass("active");
  }
  else{
  	$(".block:eq(0)").addClass('active');
  }
}

var interval = setInterval(nextTicker, 1000);

// To stop flooding 
setTimeout(function(){
  window.clearInterval(interval)
}, 20*1000)
.active{
  background: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
    <div class="block active">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
    <div class="block">
        <p>content</p>
    </div>
</div>

5 Comments

in the question he has mentioned as "infinitely loop on these elements (block)" but in you case user has to click on the next prev btns. I think he want something like news ticker, where the content becomes visible one by one periodically
In that case, @eisbehr's answer is appropriate. Still I'll update my answer to show both cases.
@atul I have updated my answer according to your comment. Hope it helps.
Just a hint, you can replace if(els.next() && els.next().hasClass('block')){ with if(els.next(".block")){
And you can compress the .next() calls from three into only one: jsfiddle.net/gxz609zz/3 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.