jQuery API

:lt() Selector

lt selector

version added: 1.0jQuery(':lt(index)')

  • index
    Zero-based index.

Description: Select all elements at an index less than index within the matched set.

index-related selectors

The index-related selectors (including this "less than" selector) filter the set of elements that have matched the expressions that precede them. They narrow the set down based on the order of the elements within this matched set. For example, if elements are first selected with a class selector (.myclass) and four elements are returned, these elements are given indices 0 through 3 for the purposes of these selectors.

Note that since JavaScript arrays use 0-based indexing, these selectors reflect that fact. This is why $('.myclass:lt(1)') selects the first element in the document with the class myclass, rather than selecting no elements. In contrast, :nth-child(n) uses 1-based indexing to conform to the CSS specification.

Additional Notes:

  • Because :lt() is a jQuery extension and not part of the CSS specification, queries using :lt() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").slice(0, index) instead.

Example:

Finds TDs less than the one with the 4th index (TD#4).

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <table border="1">

  <tr><td>TD #0</td><td>TD #1</td><td>TD #2</td></tr>
  <tr><td>TD #3</td><td>TD #4</td><td>TD #5</td></tr>

  <tr><td>TD #6</td><td>TD #7</td><td>TD #8</td></tr>
</table>
<script>$("td:lt(4)").css("color", "red");</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :lt() Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :lt() Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://www.bilheteria.com Leonardo

    I want to select from line 3, line 6 would have something like:

    $(“tr:lt( 3, 6)”).css(“color”, “red”);

    something like “between “

    • http://www.learningjquery.com/ Karl Swedberg

      Try $('tr').slice(2,5) …
      http://api.jquery.com/slice/

      • leonardocesar

        Tks, jQuery is amazing!!!!
        “I'm currently developing a plugin paging tables with the smallest possible code”

  • Htemg

    The folowwing code worked on jQuery 1.4.2 :

    $('span[title^="Hello"]+div:lt(3)').width('592px');

    It doesn't work on jQuery 1.4.3.

    Please Help

    • http://www.learningjquery.com/ Karl Swedberg

      If this appears to be a bug, please file a bug report at http://bugs.jquery.com

      thanks

  • Tessar

    Can I replace the number inside :lt(), say, var temp=5, $***:lt(temp)? var temp could be a variable set by user from html

  • Tessar

    Can I replace the number inside :lt(), say, var temp=5, $***:lt(temp)? var temp could be a variable set by user from html