jQuery .end() Method

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

What Is .end()?

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

Remember
$("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)
NeedUse
Restore previous set.end()
Keep previous + current.addBack()
Narrow into descendants.find()
Reuse without chainingSave $("…") in a variable
Pop more than one level.end().end()

How to Use .end()

Narrow, act, then pop back so the next step starts from the earlier set.

  1. Start with a broad selection (e.g. $("ul.first") or $("p")).
  2. Traverse or filter (.find(), .filter(), .eq(), …) — pushes a new set.
  3. Act on the narrowed set (.append(), .css(), …).
  4. Call .end() to restore the previous set.
  5. Continue the chain from that restored set.
PieceRequired?Role
Prior pushYesA filtering/traversal method on the stack
.end()YesPop one level — no arguments
Return value—Previous jQuery set
Variable alternativeOptionalAvoids .end() when not chaining

Pseudocode

Pseudocode
stack = [original set]
call .find(…) / .filter(…) / …
  → push narrowed set onto stack

call .end()
  → pop current set
  → return previous stack entry

// Like closing a nested block:
//   find(…) { … } end()
//   find(…) { … } end()

Cheat Sheet

Stack pop vs merge at a glance.

TopicDetail
Syntax.end() — no arguments
SincejQuery 1.0
EffectPop one stack level
vs .addBack()Replace with previous vs union
Best withLong chains after .find() / .filter()
Without chainingReuse a saved variable instead
DOM change?No — collection only

Live Preview

Narrow into .foo, then .end() and search for .bar from the list again.

  • list item 1 (foo)
  • list item 2
  • list item 3 (bar)
  • list item 1 (foo)
  • list item 2 (bar)
  • list item 3

Official idea: after .end(), the second .find() runs on ul.first again — not only inside .foo.

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

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.
  4. Find bar. Search again from the list — not only inside .foo.

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.
  4. Mark p. The final .append() hits p, not the spans alone.

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.
  4. Pick. Need only the earlier set? end. Need both? addBack.

Example 4 — Nested Finds: Two .end() Calls

Each .end() closes one filtering “block.”

jQuery
$("#wrap")
  .find("ul").append(" [ul]")
    .find("li").append(" [li]")
    .end()                 // back to ul
  .end()                   // back to #wrap
  .append(" [wrap]");
Try It Yourself

How It Works

  1. First find. Narrow to ul and mark it.
  2. Second find. Narrow further to li and mark those.
  3. First end. Pop back to the ul set.
  4. Second end. Pop back to #wrap for the final mark.

Example 5 — Same Result Without .end()

Official tip: store the set when you are not chaining.

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

How It Works

  1. Save. Keep ul.first in $list.
  2. First find. Search from that variable.
  3. Second find. Search again from the same variable.
  4. When. Prefer variables for clarity; prefer .end() for one tight chain.

Edge Cases & Pitfalls

Stack mistakes that leave you on the wrong set.

vs addBack

Wrong tool for “both groups”

.end() drops the narrowed set. If you need parent + children together, use .addBack().

count

One pop per .end()

Two nested .find() calls need two .end() calls to reach the original set.

empty

Extra .end() can empty the set

Calling .end() with nothing left to restore may leave an empty collection. Match pops to pushes.

style

Trailing .end() is optional

Official note: a final .end() before discarding the object is symmetry only — one extra call.

Browser Support

.end() has been part of jQuery since 1.0. It works in every browser that runs your loaded jQuery version (1.x, 2.x, and 3.x).

✓ Stable · jQuery 1.0+

jQuery .end()

Supported across jQuery 1.x, 2.x, and 3.x. Stack pop behavior is jQuery-specific and pairs with traversal methods like .find().

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

Bottom line: Safe for chaining. Remember: .end() restores the previous set only — use .addBack() when you need a union.

Performance Notes

ApproachCostBest for
.end()CheapPop stack in a chain
Saved variableOften clearerReuse the same root several times
Trailing decorative .end()Tiny extra callOptional readability only
Re-query with $()CostlierAvoid when .end() or a variable works

Prefer .end() or a saved variable over selecting the same root again with a new $() call in the middle of a chain.

Key Takeaways

  • Role: pop the stack and restore the previous set.
  • vs addBack: previous only vs previous + current.
  • Chaining: ideal after .find() / .filter() blocks.
  • Next: .pushStack() — how sets get onto the stack.

One line: after you narrow a set, .end() closes that step and puts you back on the previous selection.

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

9 people found this page helpful