jQuery .end() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.end() (since jQuery 1.0) ends the most recent filtering step and returns the matched set to its previous state. Traversal methods push a new set onto an internal stack; .end() pops one level. It takes no arguments.

Syntax

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

jQuery
$("ul.first").find(".foo").append(" [foo]").end()
             .find(".bar").append(" [bar]");   // official idea

$("p").find("span").end().append(" [p]");     // back to paragraphs

// end()     → previous set only (replace)
// addBack() → previous + current (union)
CallWhat it does
Prior pushA filtering/traversal method on the stack
.end()Pop one level — no arguments
Return valuePrevious jQuery set
Variable alternativeAvoids .end() when not chaining

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

Parameters

ParameterDescription
Step 1Start with a broad selection (e.g. $("ul.first") or $("p")).
Step 2Traverse or filter (.find(), .filter(), .eq(), …) — pushes a new set.
Step 3Act on the narrowed set (.append(), .css(), …).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Official: find → end → find

Style two nested searches from the same list root (jQuery API idea).

jQuery
$("ul.first")
  .find(".foo").append(" [foo]")
  .end()
  .find(".bar").append(" [bar]");
Try It Yourself

How It Works

  1. Root. Start at ul.first.
  2. Find foo. Mark matching items inside that list.
  3. end. Pop back to ul.first.

Example 2 — Official: $("p").find("span").end()

Act on spans, then restore paragraphs and mark them.

jQuery
$("p")
  .find("span").append(" [span]")
  .end()
  .append(" [p]");
Try It Yourself

How It Works

  1. Select. Start with every p.
  2. find. Narrow to span children and mark them.
  3. end. Pop back to the paragraphs.

Example 3 — .end() vs .addBack()

Pop restores the parent; merge keeps parent and children.

jQuery
var a = $("#box").find("p").end().length;     // 1 (the box)
var b = $("#box").find("p").addBack().length; // 1 + paragraphs
$("#out").text("end → " + a + "\\naddBack → " + b);
Try It Yourself

How It Works

  1. Same start. Both chains find paragraphs inside #box.
  2. end. Drops the paragraphs — set is the box again.
  3. addBack. Keeps paragraphs and the box.

Frequently Asked Questions

.end() ends the most recent filtering operation and returns the matched set to its previous state. It pops one level off jQuery's internal selection stack. Available since jQuery 1.0. It takes no arguments.
.end() restores only the previous set — the narrowed subset leaves the jQuery object. .addBack() unions previous + current so both groups remain. Use .end() when you only need the earlier selection.
When chaining: style a nested find, then .end() to return to the parent set and continue — without storing the original selection in a variable.
Yes. Each .end() pops one stack level. Two .find() calls can be paired with two .end() calls, like closing nested blocks.
Usually no. Official docs: without chaining you can reuse a saved jQuery object. .end() shines when you keep everything in one chain.
No. A final .end() before discarding the object is optional — some developers add it for visual symmetry; it is one extra call.

Next: .pushStack() Method

See how jQuery pushes new element sets onto the internal stack.

.pushStack() Method 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.

9 people found this page helpful