.replaceWith(newContent) replaces each matched element with new content (since jQuery 1.2). Target comes first; content is the argument — the reverse of .replaceAll(). A function form was added in 1.4.
Remember
$("p").replaceWith("<b>Paragraph.</b>")
$("div.second").replaceWith("<h2>New heading</h2>")
$("div.third").replaceWith($(".first")) // move existing node
// same DOM result, reversed syntax:
$("<b>Paragraph.</b>").replaceAll("p")
// returns the removed set — not the new nodes
var $gone = $("#status").replaceWith("<strong>Done</strong>");
Select what to remove, pass the replacement, swap them out.
Select the element(s) you want to replace.
Pass new content: HTML string, element, array, jQuery object, or function.
Call .replaceWith(newContent) — each match is swapped.
Remember: the return value is the removed set.
Piece
Required?
Role
jQuery collection
Yes
Elements to replace (targets)
newContent
Yes*
HTML, element(s), or jQuery
function
Alt*
Per-element replacement (1.4+)
Return
—
Original removed set
* Pass either newContent or a function, not both.
Pseudocode
Pseudocode
targets = matched elements
content = newContent // or content = fn.call(each target)
for each t in targets:
remove t (and its data/events)
if first target:
put content in t's place // move if already in DOM
else:
clone content into t's place
return original targets // removed set, not the new nodes
// Same idea as .replaceAll(), reversed syntax:
// target.replaceWith(content) ≡ content.replaceAll(target)
Reference
Cheat Sheet
Keep these facts handy while swapping elements.
Topic
Detail
Syntax
.replaceWith(newContent) or .replaceWith(fn)
Since
1.2 (content) · 1.4 (function)
vs .replaceAll()
Target-first vs content-first
Return
Removed set (not replacements)
One match
Existing nodes move
Many matches
Clones after the first
Cleanup
Data and events on removed nodes
vs .html()
Whole element vs inner content
Try it
Live Preview
Run official-style replaceWith actions on a tiny demo tree and see matched elements swap for new content.
Browser demo of .replaceWith() — same idea as the examples below.
Result
Ready — pick an action and run.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Move, not clone. Hello leaves its old spot and fills third’s place.
Official rule. Single location → move.
Example 4 — replaceWith vs replaceAll
Same swap; pick target-first or content-first syntax.
jQuery
// Target first
$("#status").replaceWith("<strong>Done</strong>");
// Content first — same DOM result
$("<strong>Done</strong>").replaceAll("#status");
Prefer .text() for user strings when building replacement nodes.
Compatibility
Browser Support
.replaceWith() is a core jQuery method (since 1.2; function form 1.4). It needs a browser DOM with jQuery loaded.
✓ jQuery 1.2+ · Browsers only
jQuery .replaceWith()
Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.replaceWith()Browsers
Bottom line: Use .replaceWith(newContent) in the browser for target-first element swaps. Prefer .replaceAll() when you build the replacement first.
Analysis
Performance Notes
Approach
Cost
Best for
.replaceWith(one)
Low
Single element swap / move
.replaceWith(many)
Higher
Clones per extra match
.html()
Often lower
Updating inside a kept shell
Prefer one clear replace over many tiny swaps in a loop. If you only need new inner content, .html() is usually cheaper.
Remember
Key Takeaways
Role: replace each matched element with new content.
Syntax: target first — mirror of .replaceAll().
Return: the removed set, not the new nodes.
Not html: the whole element is swapped, not just its insides.
One line:$(target).replaceWith(content) swaps matched elements for content — target-first twin of .replaceAll().
Frequently Asked Questions
.replaceWith(newContent) removes each matched element and inserts new content in its place. newContent can be HTML, a DOM element, an array, a jQuery object, or a function (since 1.4). Returns the original removed set. Available since jQuery 1.2.
Same DOM result, reversed syntax. Target first: $('p').replaceWith('<b>Hi</b>'). Content first: $('<b>Hi</b>').replaceAll('p'). Prefer replaceWith when the target is already selected.
The original jQuery object for the removed elements — not the new replacements. Since jQuery 1.9 it always returns that original set.
One matched target: an existing replacement node moves. Multiple matched targets: clones for every match after the first.
Data and event handlers on the removed matched nodes are cleaned up. Replacement elements keep their own handlers.
.html() changes what is inside an element; the outer tag stays. .replaceWith() removes the entire matched element and puts the replacement in its place.