JavaScript
Article
By Sam Deering

jQuery addClass() Function Example

By Sam Deering

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' : '');
});
Recommended
Sponsors
Get the latest in JavaScript, once a week, for free.