jQuery .first() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
First match

What You’ll Learn

The .first() traversing method reduces the current jQuery collection to the first element — index 0. This tutorial covers the official list highlight demos, compares .first() with .eq(0) and .last(), and shows how to chain after .filter() and .find().

01

Syntax

.first()

02

No args

Zero params

03

Index 0

First in set

04

vs .eq(0)

Same result

05

vs .last()

Opposite end

06

Since 1.4

Core API

Introduction

After selecting multiple elements — list items, table rows, tabs, cards — you often need just the first one. Maybe you want to highlight the top menu item, focus the first input in a form, or style the leading row in a table. The .first() method picks that element in one readable step.

Available since jQuery 1.4, .first() takes no arguments. It constructs a new jQuery object from the element at index zero in the current set. It is shorthand for .eq(0) — same result, clearer intent when you always want the leading element.

Understanding the .first() Method

Given a jQuery object representing zero or more DOM elements, .first() returns a new jQuery object containing only the first element in that set. The index is always zero-based — the first matched element is at position 0.

If the collection is empty, .first() returns an empty jQuery object. If the collection has one element, that element is returned unchanged in a new wrapper. The method does not search the DOM again — it only looks at the current jQuery object.

💡
Beginner Tip

$("li").first() is equivalent to $("li").eq(0). Use .first() when readability matters; use .eq(0) when you are already working with index numbers in the same chain.

📝 Syntax

General form of .first:

jQuery
.first()

Parameters

  • None — .first() does not accept any arguments.

Return value

  • A new jQuery object containing the first element in the current set, or an empty set if there are no matched elements.

Official jQuery API list example

jQuery
$( "li" ).first().css( "background-color", "red" );

// Red background on list item 1 only

⚡ Quick Reference

GoalCode
First element in set$("li").first()
Same as eq(0)$("li").eq(0)
Last element in set$("li").last() or .eq(-1)
First after filtering$("li").filter(".active").first()
First descendant match$(".form").find("input").first()
Selector at query time$("li:first")

📋 .first() vs .eq(0) vs .last() vs :first

Four ways to target the first element — readability, index, and timing.

.first()
index 0

Readable shorthand; no arguments

.eq(0)
explicit

Same result; pairs with .eq(-1)

.last()
final

Opposite end — last element only

:first
selector

Filter at initial query time

Examples Gallery

Examples 1–2 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for first-element selection.

Example 1 — Official Demo: Red Background on First List Item

Apply a red background to the first li in a simple list — the core official demo.

jQuery
$( "li" ).first().css( "background-color", "red" );
Try It Yourself

How It Works

$("li") matches all five items. .first() narrows to index 0 — the first list item — before .css() runs.

Example 2 — Official Demo: Highlight First ul li

Add a yellow highlight class to the first list item inside a ul — official interactive demo pattern.

jQuery
$( "ul li" ).first().addClass( "highlight" );
Try It Yourself

How It Works

When multiple ul blocks exist, $("ul li") flattens all list items into one collection. .first() picks the very first li in document order.

📈 Practical Patterns

Equivalence with .eq(0), post-filter selection, and descendant chains.

Example 3 — .first() vs .eq(0)

Both methods target the same element — compare readability on identical markup.

jQuery
$( "#first-demo li" ).first().css( "color", "green" );

$( "#eq-demo li" ).eq( 0 ).css( "color", "green" );



// Identical result — different syntax
Try It Yourself

How It Works

Internally, .first() is implemented as a call to .eq(0). Choose .first() for clarity; choose .eq(0) when mixing with other index values in the same chain.

Example 4 — .first() After .filter()

Get the first active list item from a filtered subset — a common real-world pattern.

jQuery
$( "li" )

  .filter( ".active" )

  .first()

  .css( "font-weight", "bold" );
Try It Yourself

How It Works

.filter(".active") narrows to matching items. .first() then picks only the leading active item — useful for tab panels, breadcrumbs, or “primary” selection logic.

Example 5 — .first() After .find()

Focus the first input inside a form section after searching descendants.

jQuery
$( ".checkout" )

  .find( "input[type='text']" )

  .first()

  .focus();
Try It Yourself

How It Works

.find() collects all matching inputs inside the container. .first() selects the top one in document order — a clean pattern for auto-focus on page load.

🚀 Common Use Cases

  • Highlight lead item$("ul.menu li").first().addClass("active").
  • Auto-focus first field$(".form").find("input").first().focus().
  • First visible tab$(".tab").filter(":visible").first().
  • Primary row styling$("tbody tr").first().addClass("lead").
  • First error message$(".error").first().show().
  • Read first data cell$("tr").first().find("td").first().text().

🧠 How .first() Picks One Element

1

Start with collection

jQuery object holds zero or more elements.

Input
2

Read index 0

Take the first element in the set.

Index
3

Wrap or empty

One-element jQuery object, or empty if none.

Result
4

Return & chain

New jQuery object — continue with .css(), .addClass(), etc.

📝 Notes

  • Available since jQuery 1.4 — no arguments accepted.
  • Equivalent to .eq(0) — shorthand for index zero only.
  • Operates on the current jQuery collection — does not re-query the DOM.
  • Empty input collection returns empty output — safe to chain.
  • Pushes onto jQuery’s internal stack — pair with .end() to restore the previous set.
  • For the last element, use .last() or .eq(-1) instead.

Browser Support

.first() has been part of jQuery since 1.4+. It relies on collection indexing with no browser-specific behavior.

jQuery 1.4+

jQuery .first()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: collection[0] wrapped back into jQuery.

100% With jQuery loaded
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
.first() Universal

Bottom line: Safe in any jQuery project. Use .first() for readability; use .eq(0) when index logic is already in play.

Conclusion

The jQuery .first() method reduces a matched collection to the single element at index zero. It is the clearest way to target the leading item when you do not need a custom index.

Remember the official list lesson: only the first item gets the red background or highlight class. Pair .first() with .filter() or .find() to pick the leading match from a narrowed set, and use .last() for the opposite end.

💡 Best Practices

✅ Do

  • Use .first() when you always want index zero
  • Chain after .filter() to get the first matching item
  • Use .last() for the final element in the same style
  • Call .end() after .first() when continuing on the full set
  • Prefer .first() over .eq(0) for readability

❌ Don’t

  • Pass arguments to .first() — use .eq(n) instead
  • Confuse :first selector timing with .first() mid-chain
  • Assume .first() searches the DOM — it only reads the current set
  • Use .first() when you need the last item — use .last()
  • Forget that an empty collection stays empty after .first()

Key Takeaways

Knowledge Unlocked

Five things to remember about .first()

Index zero — one element.

5
Core concepts
0 02

.eq(0)

Same

Equiv
03

Empty

Safe chain

Edge
04

.last()

Opposite

Compare
🔗 05

Chain

After filter

Pattern

❓ Frequently Asked Questions

.first() reduces the current jQuery collection to the first element in the set — index 0. It returns a new jQuery object containing that single element, or an empty set if the collection was already empty.
No. .first() accepts no parameters. It always selects the element at position zero in the current jQuery object. For other positions, use .eq(n) instead.
They produce the same result — both return the first element as a jQuery object. .first() is a readable shorthand when you always want index zero; .eq(0) is explicit about index-based selection and pairs naturally with .eq(-1) for the last element.
.first() picks the element at index 0. .last() picks the final element in the set — equivalent to .eq(-1). They are complementary shortcuts for the two ends of a collection.
.first() on an empty jQuery object returns another empty jQuery object with length 0. No error is thrown — chained methods simply do nothing.
:first is a jQuery selector pseudo-class used at query time — $('li:first'). .first() is a method on an existing collection — $('li').first(). Prefer .first() when you already have a jQuery object from .filter(), .find(), or another chain step.
Did you know?

jQuery’s .first() and .last() were added together in version 1.4 as readable shortcuts for .eq(0) and .eq(-1). They push onto the internal selection stack like other filtering methods, so you can call .end() afterward to return to the full matched set.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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