Feb 26, 2012 - CSS, jQuery    Comments Off on jQuery add classes to lists and tables

jQuery add classes to lists and tables

jQuery(document).ready(function($) {	
	$('ul li:first-child').addClass('first-child');
	$('ul li:last-child').addClass('last-child');
	$('ul li:nth-child(even)').addClass('even');
	$('ul li:nth-child(odd)').addClass('odd');
	$('table tr:first-child').addClass('first-child');
	$('table tr:last-child').addClass('last-child');
	$('table tr:nth-child(even)').addClass('even');
	$('table tr:nth-child(odd)').addClass('odd');
	$('tr td:first-child').addClass('first-child');
	$('tr td:last-child').addClass('last-child');
	$('tr td:nth-child(even)').addClass('even');
	$('tr td:nth-child(odd)').addClass('odd');
});

Or better yet:

jQuery(document).ready(function($) {	
	$('*:first-child').addClass('first-child');
	$('*:last-child').addClass('last-child');
	$('*:nth-child(even)').addClass('even');
	$('*:nth-child(odd)').addClass('odd');
});

Comments are closed.