I have a DOM as shown below in which to add/remove class only after third anchor element (only 4th and 5th element).
<div class="featured-block">
<a href="" class="featured-block__item cf"> <!-- 1st element -->
</a>
<a href="" class="featured-block__item cf"> <!-- 2nd element -->
</a>
<a href="" class="featured-block__item cf"> <!-- 3rd element -->
</a>
<a href="" class="featured-block__item cf"> <!-- Add/remove (4th element)-->
</a>
<a href="" class="featured-block__item cf"> <!-- Add/remove (5th element)-->
</a>
</div>
I have used the following JS code in order to add/remove class featured-block__item-active in the DOM above at the 4th and 5th position.
The problem which I am having right now is that it adds/removes the class at 4th and 5th position and then adds/removes class again at 1st/2nd/3rd position (which I don't want).
document.querySelectorAll('.featured-block .featured-block__item')[3].classList.add('featured-block__item-active'); // Line Z (Adding at 4th element)
const pics = document.querySelectorAll('.featured-block .featured-block__item');
function toggleClass() {
const activePic = document.querySelector('.featured-block__item.featured-block__item-active');
const activeIndex = Array.prototype.indexOf.call(pics, activePic);
const nextIndex = activeIndex === lastPic ? 0 : activeIndex + 1;
const nextPic = pics[nextIndex];
setTimeout(() => activePic.classList.remove('featured-block__item-active'), transitionDelay); // Line A
setTimeout(() => nextPic.classList.add('featured-block__item-active'), totalDelay); // Line B
}
setInterval(toggleClass, intervalDelay);
Problem Statement:
I am wondering what changes I should make in the JS above so that it add/remove featured-block__item-active class only at 4th and 5th position.
I think, I need to make changes at Line Z, Line A and Line B.