JavaScript - - By Sam Deering

jQuery addClass() Function Example

jQuery code snippet to add a css style for elements with the .addcss class but use a different style for first, second and third elements (multiples of 3).

$('.addcss').each(function(i){
	//value of n can be changed to mutiples of whatever you wish
	var n = i % 3;
	$(this).addClass(
	n == 0 ? 'cssstyle1' :
	n == 1 ? 'cssstyle2' :
	n == 2 ? 'cssstyle3' : '');
});
Sponsors