jQuery(document).ready(function($){ function active_next() { var current = jQuery('.active'); // get the current highlighted var next = current.next(); var parent = $('.dynamic__text'); // if no next then we're at the end if( !next.length ) { next = current.parent().find('span:first'); // get the first li element in the parent } next.addClass('active').removeClass('inactive'); current.removeClass('active').addClass('inactive'); /* let span_active = next.width(); parent.width(span_active);*/ } var myinterval = setInterval( active_next, 3000 ); });