JQuery Selector first-child

From CoderLearner
Jump to: navigation, search

Description

Select all elements that are the first child of the parent. It can match more than one.

Syntax

$(':first-child')

$('span:first-child')

$('table tr:first-child')

$('li:first-child')

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() {
            $('table tr:first-child').css('background-color','red');
         });
      </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>
      <table>
         <tr>
            <th>Group No</th>
            <th>Name</th>
            <th>Total</th>
         </tr>
         <tr>
            <td>1451</td>
            <td>Lake Group</td>
            <td>50003</td>
         </tr>
         <tr>
            <td>3451</td>
            <td>City Group</td>
            <td>5002543</td>
         </tr>
      </table>
   </body>
</html>

Try It