JQuery Selector eq

From CoderLearner
Jump to: navigation, search

Description

Select elements based on the match index. An index starts from 0.

Syntax

$('li:eq(0)');

$('tr:eq(0)');

Example 1

<!DOCTYPE html>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>

      <script>
         $().ready(function() {
            $('tr:eq(0)').css({'background-color':'blue','color':'white'});
         });
      </script>
   </head>
   <body> 
      <table border="1">
         <tr>
            <th>No</th>
            <th>Name</th>
            <th>Age</th>
         </tr>
         <tr>
            <td>1</td>
            <td>Nanak</td>
            <td></td>
         </tr>
         <tr>
            <td>2</td>
            <td>Lilo</td>
            <td></td>
         </tr>
         <tr>
            <td>3</td>
            <td>Bob</td>
            <td>14</td>
         </tr>
      </table>
   </body>
</html>

Try It