jqueryhtml-listseachaddclass

Add class with numbers to each li elements, jquery


Add class with numbers to each li elements, jquery.

Here is a sample of my code:

$('li').each(function(i) {

$(this).addClass(i);

});

There is a way to add class only with numbers, i know i am able to add class with this way $(this).addClass('something'+i); but instead i want only with numbers like:

<li class='1'></li>

Thank you!


Solution

  • Instead od $(this).addClass(i); try using $(this).addClass(i.toString());

    But, having numbers as class names will get you into trouble... I just demonstrated how you can do this with jQuery.