Also, you really don't need two counters (i and j).... var rows = $('#foobar tbody tr:visible'); for (var i = 0; i < rows.length; i++){ if ((i + 1) % 2 == 0) { rows.eq(i).addClass('roweven'); } else { rows.eq(i).addClass('rowodd'); } }
However, don't use the for loop, you have jQuery's each method... $('#foobar tbody tr:visible').each(function(i) { if ((i+1) % 2 === 0) { $(this).addClass('roweven'); } else { $(this).addClass('rowodd'); } });