jQuery: Assign class based on index
css, html, javascript, jquery
Solution
But avoid to start class or id with number;
$('ul.nav li').each(function(index){
$(this).addClass('at_'+ index);
});
Problem
Using jQuery, is it possible to assign a class to an object based on its index? For example, if I have an `ul` with five list items, how would I assign the class "0" to the first item, "1" to the second item, "2" to the third, item and so on? This was my first attempt: ``` $('ul.nav li').each(function(index){ $(this).addClass(index); }); ```