I want to change class on click on a tag.
The requirement is when we click on tag whichever div has active should replace with second and then inside next li div which has class name second should replace with active. i made small function but it is not working properly.
<div class="wrap">
<ul>
<li>
<div class="first">
<div class="second"></div>
</div>
</li>
<li>
<div class="first">
<div class="active"></div>
</div>
</li>
<li>
<div class="first">
<div class="second"></div>
</div>
</li>
</ul>
<a href="#" id="next">Next</a>
</div>
$(function(){
$('#next').click(function (){
$('ul').find('li').each(function (){
if($(this).find('div').hasClass('active')){
$(this).removeClass('active');
$(this).addClass('second');
$(this).next().closest('div').next().attr('class','active');
}
})
})
})
This should work for you:
Live DEMO
If you want to start over at the first li after reaching the bottom, this should work for you:
Live DEMO