jQuery replaceAll() Method

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

What Is replaceAll()?

.replaceAll(target) replaces each target element with the matched set (since jQuery 1.2). Content comes first; the target is the argument — the reverse of .replaceWith().

Remember
$("<b>Paragraph.</b>").replaceAll("p")
$("<h2>New heading</h2>").replaceAll(".inner")
$(".first").replaceAll(".third")   // move existing node

// same DOM result, reversed syntax:
$("p").replaceWith("<b>Paragraph.</b>")

// replaces the whole element — not just inner HTML
NeedUse
Content first, replace targets$(content).replaceAll(target)
Target first$(target).replaceWith(content)
Change inside only.html()
Delete without replacement.remove()

How to Use replaceAll()

Build or select the replacement, pass the target(s), swap them out.

  1. Create or select the replacement content.
  2. Choose the target: selector, element, array, or jQuery object.
  3. Call .replaceAll(target) — each target is swapped for the content.
  4. Chain on the returned inserted set if you need further steps.
PieceRequired?Role
jQuery collectionYesReplacement content
targetYesElement(s) to replace
Return—Inserted replacement set

Pseudocode

Pseudocode
content = matched elements (or new HTML nodes)
targets = resolve(target)

for each t in targets:
    remove t (and its data/events)
    if first target:
        move content into t's place
    else:
        clone content into t's place

return inserted replacements

// Same idea as .replaceWith(), reversed syntax:
// content.replaceAll(target)  ≡  target.replaceWith(content)

Cheat Sheet

Keep these facts handy while swapping elements.

TopicDetail
Syntax.replaceAll(target)
SincejQuery 1.2
vs .replaceWith()Content-first vs target-first
One targetExisting nodes move
Many targetsClones after the first
Target cleanupData and events removed
vs .html()Whole element vs inner content

Live Preview

Run official-style replaceAll actions on a tiny demo tree and see targets swap for new content.

Browser demo of .replaceAll() — same idea as the examples below.

Result
Ready — pick an action and run.

jQuery Examples

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

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>
$("<b>Paragraph.</b>").replaceAll("p");
Try It Yourself

How It Works

  1. Build content. A new <b> is created.
  2. Match targets. Every p is selected for replacement.
  3. Swap. Each paragraph is removed; a bold node takes its place.
  4. Clones. Multiple targets get copies of the replacement.

Example 2 — Official Demo: Replace .inner

Create a heading and replace every .inner element with it.

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

How It Works

  1. New heading. HTML is parsed into an h2.
  2. Three targets. All .inner divs match.
  3. Each replaced. The old divs leave; headings fill their slots.
  4. Whole element. Not just the text inside — the node itself is gone.

Example 3 — Official Demo: Move an Existing Node

Use one existing element as the replacement for another — it moves.

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

How It Works

  1. Select content. $(".first") is the Hello block.
  2. One target. .third is removed.
  3. Move, not clone. Hello leaves its old spot and fills third’s place.
  4. Official rule. Single location → move.

Example 4 — replaceAll vs replaceWith

Same swap; pick content-first or target-first syntax.

jQuery
// Content first
$("<strong>Done</strong>").replaceAll("#status");

// Target first — same DOM result
$("#status").replaceWith("<strong>Done</strong>");
Try It Yourself

How It Works

  1. Build first. replaceAll reads well when the new node is ready.
  2. Select first. replaceWith reads well when the old node is selected.
  3. Same swap. #status is removed; <strong> takes its place.
  4. Not html. The outer element changes, not only the text inside.

Example 5 — Safe Text Build

Create the node, set text safely, then replace — avoids injecting raw HTML.

jQuery
var label = "Hello <user>";
$("<p>").text(label).replaceAll("#msg");
// Result: <p>Hello &lt;user&gt;</p> — tags shown as text
Try It Yourself

How It Works

  1. Empty shell. $("<p>") creates a paragraph without HTML content.
  2. Safe text. .text(label) escapes < so it is not markup.
  3. Replace. #msg is swapped for the new paragraph.
  4. Safer path. Prefer this over pasting user strings into $("<…>").

Edge Cases & Pitfalls

Check these before swapping nodes in real pages.

cleanup

Targets lose data and events

Handlers on replaced nodes are cleared. Tear down carefully if you need custom cleanup.

move

One target moves nodes

Existing replacements leave their old spot. Multiple targets clone after the first.

scope

Whole element, not inner HTML

To change only contents, use .html().

xss

Untrusted HTML is unsafe

Prefer .text() for user strings when building replacement nodes.

Browser Support

.replaceAll() is a core jQuery method (since 1.2). It needs a browser DOM with jQuery loaded.

✓ jQuery 1.2+ · Browsers only

jQuery .replaceAll()

Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery. Not for Node.js without a DOM.

100% Browser support
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
.replaceAll() Browsers

Bottom line: Use .replaceAll(target) in the browser for content-first element swaps. Prefer .replaceWith() when the target is already selected.

Performance Notes

ApproachCostBest for
.replaceAll(one)LowSingle element swap / move
.replaceAll(many)HigherClones per extra target
.html()Often lowerUpdating 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.

Key Takeaways

  • Role: replace each target with the matched content.
  • Syntax: content first — mirror of .replaceWith().
  • Move vs clone: one target moves; many targets clone.
  • Not html: the whole element is swapped, not just its insides.

One line: $(content).replaceAll(target) swaps targets for content — content-first twin of .replaceWith().

Frequently Asked Questions

.replaceAll(target) replaces each target element with the elements in the current jQuery collection. Content comes first in the syntax. Returns the inserted replacements. 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 replaceAll when you build the replacement first.
One target location: existing replacement nodes move (not clone). Multiple targets: clones for every target after the first.
Data and event handlers on the removed target nodes are cleaned up. The replacement elements keep their own handlers.
A CSS selector, jQuery object, DOM element, or array of elements identifying what to replace.
.html() changes what is inside an element; the outer tag stays. .replaceAll() removes the entire target and puts the replacement in its place.

Next: replaceWith()

Learn the target-first syntax that performs the same replacement.

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