Less than (:lt(n))

Description and Syntax

$(':lt(index)')

selects all elements at an index less than n within the matched set.

  • $('li:lt(2)') selects all <li> elements preceding the third one
  • $('.myclass:lt(1)') selects all elements with the class myclass preceding the second one

Examples

The following code selects the first two table cells.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--  www  . j av a 2  s .c o m-->
           $("td:lt(2)").css("color", "red");
        });
    </script>

  </head>
  <body>
    <body>
    <table>
        <tr><td>First</td></tr>
        <tr><td>Middle</td></tr>
        <tr><td>Last</td></tr>
    </table>


    </body>
</html>

Click to view the demo

The code above generates the following result.

Less than (:lt(n))