JQuery Selector first

From CoderLearner
Jump to: navigation, search

Description

It is similar to $(':first-child') but this selector will select a single element. To select multiple elements use $(':first-child'). This selector is equal to $(':eq(0)') or $(':lt(1)').

Syntax

$(':first')

$('li:first')

$('span:first')

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:first').css('background-color','yellow');
         });
      </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