Assign class based on index - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:attr

Description

Assign class based on index

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/*w ww.  j a  va  2s .  c o m*/
$('ul.nav li').each(function(index){
  $(this).attr('class', 'class'+index);
});
    });

      </script> 
 </head> 
 <body> 
  <ul class="nav"> 
   <li>1</li> 
   <li>2</li> 
   <li>3</li> 
  </ul>  
 </body>
</html>

Related Tutorials