jQuery HTML Element How to - Modify a li element using its position index in the list








Question

We would like to know how to modify a li element using its position index in the list.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<style type='text/css'>
.important {<!--  w  w w  .j  a v  a 2 s  .  c  om-->
  background-color: red;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $("ul li").eq(2).addClass("important");
});
</script>
</head>
<body>
  <ul>
    <li>0</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
  </ul>
</body>
</html>

The code above is rendered as follows: