.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)
spans get [span]
paragraphs get [p]
(after .end() the set is <p> again)
How It Works
Select. Start with every p.
find. Narrow to span children and mark them.
end. Pop back to the paragraphs.
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);
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.