By clicking a 'next' or 'prev' button, I am trying to add a class ('.active') to the next or previous image. Why does my code currently only works one time --it adds the class to the next image-- and then stops working?
Link:
http://jordy.studio/paintings_new
$('#thumbnails').each(function() {
$('#thumbnails').find('img').first().addClass('active');
});
$("#next").click(function() {
$('#thumbnails').find('img').hasClass('active').removeClass('active').first().next('img').addClass('active');
});
$("#prev").click(function() {
$('#thumbnails').find('img').removeClass('active').prev('img').first().addClass('active');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="thumbnails">
<img class="active" src="img_01.jpg">
<img class=" " src="img_02.jpg">
<img class=" " src="img_03.jpg">
<img class=" " src="img_04.jpg">
</div>
<footer>
<p id="prev">prev</p>
<p id="next">next</p>
</footer>
Trying to use jQuery .siblings resulted in all next images being given the class '.active'.
Just spent a good night on trying to get this to work I hope someone can explain to me what I am doing wrong. Although enthusiast about jQuery I have not much experience. Everyday is a school day!