Loop through elements with incrementing class name
var i = 0; $('.picBoxGrid').find('.picBox').each(function () { if (i < 4) { i++; } else { i = 1; } $(this).addClass('picBoxColor-' + i); });