jQuery :lt() Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
jQuery extension · deprecated 3.4

What You’ll Learn

The :lt(index) selector matches every element whose zero-based index in the matched set is less than index. Negative indices since 1.8. jQuery extension since 1.0; deprecated in 3.4 — prefer .slice(0, index).

01

Syntax

td:lt(4)

02

< index

Strictly before

03

Official

yellow + red

04

Negative

Since 1.8

05

.slice()

(0, n)

06

:lt(1)

Same as :first

Introduction

Index filters let you target a subset of an already matched collection — style the first few table cells, highlight leading list items, or trim trailing rows from a batch operation. The jQuery :lt(index) pseudo-class selects elements before a given position in the matched set.

jQuery has supported :lt() since version 1.0; negative indices since 1.8. Official documentation states that index-related selectors filter the set matched by what precedes them and use 0-based indexing like JavaScript arrays — unlike CSS :nth-child(n), which is 1-based.

Understanding the :lt() Selector

Think of :lt(n) as “everything before index n in this matched set”:

  • $("td:lt(4)") → TD #0, #1, #2, #3 — indices 0 through 3 (official demo yellow cells).
  • $("li:lt(2)") → the first two list items — indices 0 and 1.
  • :lt(1) matches index 0 only — equivalent to :first and :eq(0).
  • Negative :lt(-2) since 1.8 — official demo colors TD #0–#6 red text.
💡
Beginner Tip

:lt(3) maps to .slice(0, 3) when migrating — pass the lt index as the second argument to .slice(). Need exactly one element at an index? Use :eq(n) or .eq(n) instead.

📝 Syntax

Official jQuery API form (since 1.0; negative since 1.8; deprecated 3.4):

jQuery
jQuery( ":lt(index)" )

// typical usage — prefix with element:
$( "td:lt(4)" )
$( "ul.nav li:lt(2)" )
$( "tr:lt(3)" )

// negative index (since 1.8):
$( "td:lt(-2)" )

// modern replacement (jQuery 3.4+ guidance):
$( "td" ).slice( 0, 4 )   // replaces td:lt(4)

Parameters

  • index — zero-based cutoff; elements with index less than this value match.
  • Negative index (since 1.8) — counts backwards from the last element in the matched set.

Return value

  • A jQuery object containing every matching element before the cutoff index.
  • Empty collection when no elements fall below the index.

Official jQuery API example

jQuery
$( "td:lt(4)" ).css( "backgroundColor", "yellow" );
$( "td:lt(-2)" ).css( "color", "red" );

⚡ Quick Reference

Goal:lt().slice()
First four TDs (#0–#3)td:lt(4)$("td").slice(0, 4)
First two nav itemsli:lt(2)$("li").slice(0, 2)
First element only:lt(1) or :first.slice(0, 1) or .first()
One exact indexUse :eq(n).eq(n)
After an index:gt(n).slice(n + 1)

📋 :lt() vs :gt(), :eq(), :first, and .slice()

Range before an index vs after vs single pick vs first element.

:lt(n)
td:lt(4)

Index < n

:gt(n)
td:gt(4)

Index > n

:eq(n)
td:eq(4)

Exactly index n

:first / :lt(1)
td:first

Index 0 only

.slice(0, n)
.slice(0, 4)

Modern :lt(4)

Examples Gallery

Example 1 follows the official jQuery td:lt(4) and td:lt(-2) demo. Examples 2–5 cover :lt(1) as :first, .slice() migration, contrast with :gt(), and navigation list trimming.

📚 Official jQuery Demo

Yellow background on TD #0–#3; red text on TD #0–#6 via :lt(-2).

Example 1 — Official Demo: td:lt(4) and td:lt(-2)

Official jQuery demo — cells #0–#3 yellow; cells #0–#6 red text.

jQuery
$( "td:lt(4)" ).css( "backgroundColor", "yellow" );
$( "td:lt(-2)" ).css( "color", "red" );
Try It Yourself

How It Works

Nine cells indexed 0–8. :lt(4) keeps indices 0–3. :lt(-2) from the end keeps every cell before the -2nd-from-end position (indices 0–6).

Example 2 — :lt(1) Same as :first

Select exactly one element — the first in the matched set.

jQuery
console.log( ":lt(1) →", $( "td:lt(1)" ).length );   // 1
console.log( ":first →", $( "td:first" ).length );     // 1
console.log( ":eq(0) →", $( "td:eq(0)" ).length );     // 1
Try It Yourself

How It Works

:lt(1) means index < 1, so only index 0 matches — identical to :first and :eq(0).

📈 Practical Patterns

Migration, before vs after cutoff, scoped nav lists.

Example 3 — Modern Code: .slice(0, 3) Instead of :lt(3)

jQuery 3.4+ guidance — :lt(3) becomes .slice(0, 3).

jQuery
// legacy (deprecated selector):
// $( "td:lt(3)" ).css( "backgroundColor", "yellow" );

// recommended:
$( "td" ).slice( 0, 3 ).css( "backgroundColor", "yellow" );
Try It Yourself

How It Works

Official docs: pass the lt index as the second argument to .slice()lt(3)slice(0, 3). For better performance, select with CSS first, then slice.

Example 4 — :lt(4) vs :gt(4)

Before an index vs after an index — complementary range filters.

jQuery
console.log( ":lt(4) →", $( "td:lt(4)" ).length );  // TD #0–#3
console.log( ":gt(4) →", $( "td:gt(4)" ).length );  // TD #5–#8
// index 4 (TD #4) matches neither
Try It Yourself

How It Works

:lt is strictly less than; :gt is strictly greater than. Together they split the matched set around a cutoff without overlapping.

Example 5 — Navigation List: ul.nav li:lt(2)

Style or highlight only the first two items in a navigation menu.

jQuery
$( "ul.nav li:lt(2)" ).addClass( "nav-primary" );
Try It Yourself

How It Works

:lt(2) keeps indices 0 and 1 — the first two list items. Modern equivalent: $("ul.nav li").slice(0, 2).

🚀 Common Use Cases

  • Table styling — highlight td:lt(4) leading columns per official demo.
  • First N itemsli:lt(3) for featured list entries.
  • Navigation menusul.nav li:lt(2) for primary links.
  • Legacy maintenance — read older code using index pseudo-classes.
  • Migration — replace with .slice(0, index) in modern projects.
  • Pair with :gt() — combine range filters for middle segments (legacy pattern).

🧠 How jQuery Evaluates :lt()

1

Build matched set

Evaluate the selector before :lt(n) — e.g. all td elements in document order.

Query
2

Assign indices

Number elements 0, 1, 2, … in the matched set — official docs use 0-based indexing.

Index
3

Filter < n

Keep elements whose index is strictly less than the argument.

Filter
4

Return collection

Chain .css(), .addClass(), or other jQuery methods.

📝 Notes

  • Available since jQuery 1.0 — negative indices since 1.8 — deprecated in 3.4.
  • Replace :lt(n) with .slice(0, n) — official migration guidance.
  • Strictly less than — :lt(4) excludes index 4 itself.
  • :lt(1) is equivalent to :first and :eq(0).
  • 0-based indexing — unlike CSS :nth-child() (1-based).
  • jQuery extension — not valid in native querySelectorAll.
  • For performance, use a CSS selector first, then .slice(0, index).
  • Prior to jQuery 1.8, a negative index was not accepted.

Browser Support

The :lt() pseudo-class is a jQuery extension — not standard CSS — and works in jQuery 1.0+ (negative indices 1.8+). It is deprecated in 3.4+; use .slice() instead. The method works in all browsers that support jQuery.

jQuery 1.0+ · deprecated 3.4

jQuery :lt() Selector

Use $("td").slice(0, 4) in new code — not td:lt(4) in selector strings.

100% jQuery only
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:lt() Deprecated

Bottom line: Migrate to .slice(0, index) — select with CSS, then filter in jQuery.

Conclusion

The :lt(index) selector matches every element whose index in the matched set is less than index — the official demo yellows TD #0–#3 and reddens TD #0–#6 via :lt(-2).

Remember 0-based indexing, the .slice(0, index) migration path, and that :lt(1) equals :first when you need only the leading element.

💡 Best Practices

✅ Do

  • Use $("td").slice(0, 4) in new jQuery code
  • Prefix with an element — li:lt(2) not bare :lt(2)
  • Pass lt index as second arg to .slice(0, n)
  • Use :eq(n) when you need exactly one index
  • Scope with a parent selector when possible

❌ Don’t

  • Use :lt() in new selector strings (deprecated 3.4)
  • Confuse :lt(n) with :gt(n) — before vs after
  • Assume 1-based indexing — lt is 0-based
  • Expect :lt to work in querySelectorAll
  • Forget :lt(4) excludes index 4 itself

Key Takeaways

Knowledge Unlocked

Five things to remember about :lt()

Elements before index n in the matched set.

5
Core concepts
0,n 02

.slice()

Migration

Tip
0 03

0-based

Like arrays

Rule
demo 04

Official

td yellow

Demo
1st 05

:lt(1)

= :first

API

❓ Frequently Asked Questions

:lt(index) selects every element whose zero-based index in the matched set is less than index. $("td:lt(4)") matches TD #0 through #3 (indices 0, 1, 2, 3) in the official demo. Available since jQuery 1.0.
Yes — :lt(1) selects elements with index less than 1, which is only index 0. That is equivalent to :first and :eq(0): exactly one element, the first in the matched set.
Since jQuery 1.8, :lt(-index) counts backwards from the last element. In the official demo, $("td:lt(-2)") turns red text on every cell before the -2nd from the end — TD #0 through #6 among nine TD elements (indices 0–8).
Yes — deprecated in jQuery 3.4. Official docs recommend removing :lt() from selectors and using .slice() instead: $("td").slice(0, 4) replaces $("td:lt(4)") — pass the lt index as the second argument to slice.
:lt() filters the jQuery matched set by 0-based position after the preceding selector. :nth-child(n) is CSS — it picks elements that are the nth child of their parent (1-based). They use different indexing models.
:lt(4) keeps indices 0, 1, 2, 3 — strictly before index 4. :gt(4) keeps indices 5, 6, 7, 8 — strictly after index 4. Index 4 itself matches neither selector.
Did you know?

The :first selector could be written as :lt(1) — both match exactly one element at index 0 in the matched set. Official docs note this equivalence when explaining index filters.

Continue to :gt() Selector

After filtering elements before an index, learn how :gt() matches everything strictly after a cutoff.

:gt() selector tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful