jQuery Selector How to - Select all elements with an index below the given one








Question

We would like to know how to select all elements with an index below the given one.

Answer


<!-- ww w.  ja v  a 2  s.  c  o m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
           $("td:lt(2)").css("color", "red");
        });
    </script>
  </head>
  <body>
    <table>
        <tr><td>First</td></tr>
        <tr><td>Middle</td></tr>
        <tr><td>Last</td></tr>
    </table>
    </body>
</html>

The code above is rendered as follows: