// count function function countElement(item, array) { var count = 0; $.each(array, function(i, v) { if (v === item) { count++; } }); return count; } // create array from ips in the list ipList = []; uaList = []; $('li.list').each(function(i) { var spans = $(this).children(); spans[0] = $(spans[0]); spans[1] = $(spans[1]); ipList[i] = spans[0].text(); uaList[i] = spans[1].text(); }); // check how many for each ip $('li.list').each(function(i) { var spans = $(this).children(); spans[0] = $(spans[0]); spans[1] = $(spans[1]); var ipText = $(spans[0]).text(); var uaText = $(spans[1]).text(); var count = countElement(ipText, ipList); //if more than one update class of span if (count > 1) { spans[0].addClass('i' + count); } var countua = countElement(uaText, uaList); //if more than one update class of span if (countua > 1) { spans[1].addClass('u' + count); } }); .u2 {background:orange;} .u3{background:purple;} .u4{background:brown;} .u5{background:pink;} .u6{background:gray;} .i2 {background:yellow;} .i3{background:cyan;} .i4{background:green;} .i5{background:red;} .i6{background:black; color: white}