jQuery :lt() Selector

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is :lt()?

:lt(index) (since jQuery 1.0) selects every element whose zero-based index in the matched set is less than index. It is a jQuery extension (not CSS) and is deprecated since 3.4. Prefer .slice(0, index).

Remember
// Preferred (official migration)
$("td").slice(0, 4);      // same as :lt(4)
$("td").slice(0, -2);     // same as :lt(-2)

// Legacy selector (deprecated 3.4)
$("td:lt(4)");
$("td:lt(-2)");           // negative since 1.8

// :lt(1) → only index 0 (like .first())
$("li").slice(0, 1);
NeedUse
First n elements$("li").slice(0, n)
All but last 2$("li").slice(0, -2)
Greater than index$("li").slice(n + 1)
Exact index$("li").eq(n)
First only$("li").first()

How to Use .slice() / :lt()

Select with CSS first, then keep elements before an index.

  1. Build a matched set with a pure CSS selector: $("td") or $("ul.nav li").
  2. Chain .slice(0, index) to keep indices 0 … index - 1.
  3. Use a negative end for “all but the last n”: .slice(0, -2).
  4. Avoid putting :lt() inside the selector string in new code.
  5. Remember: indexing is 0-based on the matched set, not CSS :nth-child.
PieceRequired?Role
CSS selectorYesBuilds the matched set
.slice(0, n)PreferredKeeps indices < n
:lt(n)LegacySame idea inside the selector (deprecated)

Pseudocode

Pseudocode
build a matched set with a CSS selector
    assign each element index 0, 1, 2, …

keep every element where index < n
    → same as .slice(0, n)

if n is negative:
    count from the end of the set
    keep everything before that end index
    → same as .slice(0, -abs)

Cheat Sheet

Keep these facts handy while filtering by index.

TopicDetail
Prefer$("elem").slice(0, n)
Legacy$("elem:lt(n)") (deprecated 3.4)
Since1.0 (negative index since 1.8)
Index model0-based on the matched set
:lt(1)Same as .first() / .eq(0)
vs :gt(n)Before n vs after n (n itself excluded)
CSS?No — jQuery extension
Official demo$("td:lt(4)") / :lt(-2)

Live Preview

Mark the first three items with .slice(0, 3), try a negative end, or count matches.

Five list items below (indexes 0–4). .slice(0, 3) keeps 0, 1, 2.

  • Item 0
  • Item 1
  • Item 2
  • Item 3
  • Item 4
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Official .slice(0, 4)

Mark the first four table cells (official :lt(4) idea, CSS-free).

jQuery
$("td").slice(0, 4).text(function (i, t) {
  return "[lt] " + t;
});
Try It Yourself

How It Works

  1. Select. $("td") builds the full cell set.
  2. Slice. .slice(0, 4) keeps indexes 0–3.
  3. Update. Those four cells get a clear text prefix.
  4. Skip. Index 4 and later stay unchanged.

Example 2 — Negative End .slice(0, -2)

Keep everything except the last two items (same as :lt(-2)).

jQuery
$("li").slice(0, -2).text(function (i, t) {
  return t + " (kept)";
});
$("#out").text("Kept " + $("li").slice(0, -2).length + " of " + $("li").length);
Try It Yourself

How It Works

  1. Set. Five list items → indexes 0–4.
  2. End. -2 means “stop before the last two”.
  3. Keep. Indexes 0, 1, 2 are marked.
  4. Drop. Indexes 3 and 4 are excluded.

Example 3 — .slice(0, 1) Same as .first()

:lt(1) keeps only index 0 — the first match.

jQuery
$("#out").text(
  "slice(0,1) → " + $("li").slice(0, 1).text() +
  " | .first() → " + $("li").first().text()
);
Try It Yourself

How It Works

  1. Rule. Index < 1 means only index 0.
  2. Equivalence. Same single element as .first().
  3. Choose. Prefer .first() when you want one item.
  4. Slice. Use .slice(0, n) when n > 1.

Example 4 — .slice(0, 4) vs After Index 4

Index 4 matches neither “less than 4” nor “greater than 4”.

jQuery
$("#out").text(
  "lt → " + $("li").slice(0, 4).length +
  " | gt → " + $("li").slice(5).length +
  " | index 4 alone → " + $("li").eq(4).length
);
Try It Yourself

How It Works

  1. lt. .slice(0, 4) → indexes 0–3.
  2. gt. .slice(5) → indexes 5+ (same idea as :gt(4)).
  3. Gap. Index 4 is excluded from both ranges.
  4. Exact. Use .eq(4) when you need that middle item.

Example 5 — Prefer .slice() Over :lt()

Same result — modern method vs deprecated selector.

jQuery
$("#out").text(
  "slice → " + $("p").slice(0, 2).map(function () {
    return $(this).text();
  }).get().join(", ") +
  " | :lt → " + $("p:lt(2)").map(function () {
    return $(this).text();
  }).get().join(", ")
);
Try It Yourself

How It Works

  1. Same set. Both keep the first two paragraphs.
  2. Method. .slice(0, 2) is the recommended form.
  3. Selector. :lt(2) still works but is deprecated since 3.4.
  4. Habit. Write new code with .slice() only.

Edge Cases & Pitfalls

Check these when the “less than” filter surprises you.

deprecated

Avoid :lt() in new selectors

Deprecated since jQuery 3.4. Prefer $("elem").slice(0, n) after a CSS selector.

index

Zero-based, exclusive end

.slice(0, 4) keeps 0–3. Index 4 is not included. This is not CSS :nth-child (1-based).

scope

The set comes first

Indexes are assigned after the preceding selector. $("ul.nav li").slice(0, 2) and $("li").slice(0, 2) can differ.

perf

Not native CSS

:lt() cannot use querySelectorAll. Selecting with CSS first, then .slice(), is faster on large pages.

Browser Support

:lt() is a jQuery extension (not standard CSS). Available since 1.0 (negative index since 1.8), deprecated in 3.4. Prefer .slice(), which works in every browser that runs your loaded jQuery version.

✓ Deprecated · jQuery 1.0+

jQuery :lt() / .slice()

The selector still works in jQuery 3.x but is deprecated. Use .slice(0, n) for new code — same result, better practice.

1.0+ jQuery since
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 3.4

Bottom line: Safe to learn for reading old code. Write new code with $("elem").slice(0, n) instead of $("elem:lt(n)").

Performance Notes

ApproachCostBest for
$("ul.nav li").slice(0, 3)CheapScoped lists (preferred)
$("li").slice(0, 3)Cheap–moderatePage-wide first n items
$("li:lt(3)")ModerateLegacy only — avoid in new code

Pure CSS selection can use the browser engine; :lt() cannot. Chain .slice() after CSS for the best of both.

Key Takeaways

  • Role: keep elements with index < n in the matched set.
  • Prefer: .slice(0, n) over deprecated :lt(n).
  • 0-based: .slice(0, 4) keeps 0–3; index 4 is out.
  • Negative: .slice(0, -2) keeps all but the last two.

One line: use .slice(0, n) to keep the first n matches — not the deprecated :lt(n) selector.

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. Available since jQuery 1.0.
Yes. :lt(1) keeps only index 0 — the same as :first and :eq(0). Prefer $("elem").first() or .slice(0, 1) in modern code.
Since jQuery 1.8, :lt(-index) counts from the end. Prefer .slice(0, -2), which keeps every element before the last two.
Yes — deprecated in jQuery 3.4. Official docs recommend .slice() instead: $("td").slice(0, 4) replaces $("td:lt(4)").
:lt() filters the jQuery matched set by 0-based position. :nth-child(n) is CSS and uses 1-based child position under a parent. Different models.
:lt(4) keeps indices 0–3. :gt(4) keeps indices after 4. Index 4 itself matches neither selector.

Next: :gt() selector

Select elements at an index greater than n in the matched set.

:gt() 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.

8 people found this page helpful