Read the docs , it already has an index!
.each( function(index, Element) )
No need for i
$('#cps-assess-form fieldset').each( function(index) { var q = $(this).find('.fieldset-wrapper').slideUp(); $('<div/>').html(markup[index]).insertAfter(q); });
The reason you failed is because i inside the function, so it reset every iteration. You will need to move it outside the function for it to work.
source share