Description: Select all elements at an index less than
index within the matched set.
version added: 1.0jQuery( ":lt(index)" )
index: Zero-based index.
version added: 1.8jQuery( ":lt(-index)" )
-index: Zero-based index, counting backwards from the last element.
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.
$( ".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.
Prior to jQuery 1.8, the
:lt(index) selector did not accept a negative value for
: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
Give TDs less than the one with the 4th index (TD#4) a yellow background and TDs less than the one with -2nd index a red text color.