JavaScript
Article

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' : '');
});

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.