$(document).ready(function(){ showCaption(1); $('.slideshow--bullet').click(function(){ var $length = $('.slideshow--bullet').length; removeCaption($length); var $id = $(this).attr('id')[1]; showCaption($id); $prev_index = $id; }); }); function showCaption($index){ setTimeout(function(){ $('.img_caption'+$index).addClass('active'); },1000); } function removeCaption($img_length){ for(var $i = 1; $i <= $img_length ; $i++){ $('.img_caption'+$i).removeClass('active'); } }