jQuery :lt() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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);
CallWhat it does
CSS selectorBuilds the matched set
.slice(0, n)Keeps indices < n
:lt(n)Same idea inside the selector (deprecated)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
0Used in .slice(0, n). Keeps indices < n
nUsed in .slice(0, n). Keeps indices < n

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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 — Index Greater Than

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

:gt() Selector — Index Greater Than tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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