jQuery replaceWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$("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>");
CallWhat it does
replaceWith()Learn jQuery .replaceWith(): replace matched elements with new content, target-first twin of replaceAll, callback form, return value, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select the element(s) you want to replace.
Step 2Pass new content: HTML string, element, array, jQuery object, or function.
Step 3Call .replaceWith(newContent) — each match is swapped.

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 Demo: Replace All Paragraphs

Swap every <p> for a bold “Paragraph.” element.

jQuery
// <p>Hello</p><p>cruel</p><p>World</p>
$("p").replaceWith("<b>Paragraph.</b>");
Try It Yourself

How It Works

  1. Select. Every p is in the matched set.
  2. Content. An HTML string builds the bold replacement.
  3. Swap. Each paragraph is removed; bold fills its slot.

Example 2 — Official Demo: Replace .second

Replace one middle .inner div with a new heading.

jQuery
// <div class="container">
//   <div class="inner first">Hello</div>
//   <div class="inner second">And</div>
//   <div class="inner third">Goodbye</div>
// </div>
$("div.second").replaceWith("<h2>New heading</h2>");
Try It Yourself

How It Works

  1. One target. Only div.second matches.
  2. New heading. HTML is parsed into an h2.
  3. Whole element. The middle div leaves; the heading takes its place.

Example 3 — Official Demo: Move an Existing Node

Use one existing element as the replacement — it moves, not clones.

jQuery
// Start: .first Hello, .second And, .third Goodbye
$("div.third").replaceWith($(".first"));
// Result: And, then Hello (moved) — .third is gone
Try It Yourself

How It Works

  1. Select target. div.third will be removed.
  2. Pass content. $(".first") is an existing DOM node.
  3. Move, not clone. Hello leaves its old spot and fills third’s place.

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.

Next: hasData() Utility — Check Element Data Cache

Check whether an element still has any jQuery data stored on it.

hasData() Utility — Check Element Data Cache 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