jQuery Selector How to - Highlight children items by index








Question

We would like to know how to highlight children items by index.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from   ww w . ja v  a2s. co m-->
    $('div p').eq(2).css('background','yellow');
});
</script>
</head>
<body>
  <div>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
  </div>
</body>
</html>

The code above is rendered as follows: