jQuery replaceAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$("<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
CallWhat it does
replaceAll()Learn jQuery .replaceAll(): replace target elements with matched content, content-first twin of replaceWith, move vs clone, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Create or select the replacement content.
Step 2Choose the target: selector, element, array, or jQuery object.
Step 3Call .replaceAll(target) — each target is swapped for the content.

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>
$("<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.

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.

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.

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() Method — Replace Matched Elements

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

replaceWith() Method — Replace Matched Elements 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