Example 1 — Official tr:first
Mark the first table row in the matched set.
$("tr:first td").prepend("[first] "); How It Works
- Match.
$("tr")finds every row. - Filter.
:firstkeeps only index 0. - Mark. Prepend a label in that row.

:first (since jQuery 1.0, deprecated 3.4) selects exactly one element — the first in the matched set, in document order. It equals :eq(0) / :lt(1). Prefer $("…").first() in new code.
Start with these common calls. Keep them short and readable.
// Legacy (still works; deprecated 3.4)
$("tr:first");
// Recommended
$("tr").first();
// Same idea
$("tr:eq(0)");
$("tr:lt(1)");
// Not the same as :first-child (can match many)
$("li:first-child"); | Call | What it does |
|---|---|
Base selector | Builds the matched set (e.g. tr, li) |
:first / .first() | Keep index 0 of that set |
Document order | Matches appear in DOM order |
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
| Parameter | Description |
|---|---|
| Step 1 | Select candidates with a pure CSS selector: $("tr"), $("ul li"). |
| Step 2 | Take the first: .first() (recommended) or legacy :first. |
| Step 3 | Chain actions — .text(), .addClass(), events. |
| Return Value | Description |
|---|---|
| See examples | Check the examples below for what this API returns in common cases. |
tr:firstMark the first table row in the matched set.
$("tr:first td").prepend("[first] "); $("tr") finds every row.:first keeps only index 0..first()Official migration path after jQuery 3.4.
$("tr").first().find("td").prepend("[first] "); .first() keeps one element.:eq(0)Official docs: :first equals :eq(0) (and :lt(1)).
var a = $("tr:first").text();
var b = $("tr:eq(0)").text();
$("#out").text("first=[" + a + "] eq=[" + b + "]"); :first and :eq(0).:lt(1) yields the same single item.Learn the mirror filter for the last element in a matched set.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
15 people found this page helpful