jQuery .replaceAll() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM replacement

What You’ll Learn

The .replaceAll() method replaces target elements with the matched set — content first, target second. This tutorial covers the official jQuery demo, new HTML replacements, moving existing nodes, comparisons with .replaceWith() and .html(), data/event cleanup on removed targets, move vs clone behavior, and five practical try-it examples.

01

Content first

Reversed

02

Swap nodes

Full replace

03

Move

Not clone

04

vs replaceWith

Pair API

05

Cleans up

Data/events

06

Since 1.2

Core API

Introduction

Sometimes you need to swap one DOM element for another — upgrade plain paragraphs to styled headings, replace deprecated markup with accessible components, or move an existing node into a new position by replacing a placeholder. jQuery’s .replaceAll() handles that in one call.

Available since jQuery 1.2, .replaceAll( target ) is the content-first counterpart to .replaceWith(). You build or select the replacement elements first, then pass a selector or jQuery object identifying what to remove. Each target node is removed from the DOM and the replacement takes its place.

Important: replaced target nodes lose their jQuery data and event handlers. When a single replacement moves into one slot, jQuery moves the node rather than cloning it. Browse the jQuery DOM hub and .insertBefore() for related content-first APIs.

Understanding the .replaceAll() Method

.replaceAll( target ) operates on the jQuery collection that becomes the new content. The target argument identifies elements to remove — a selector string, DOM element, array of elements, or jQuery object. The method returns the replacement collection for chaining.

Think of it as reversed .replaceWith(): $( "p" ).replaceWith( "<b>Hi</b>" ) starts from the paragraphs; $( "<b>Hi</b>" ).replaceAll( "p" ) starts from the new content. Both produce the same final DOM when used correctly.

💡
Beginner Tip

The official jQuery demo runs $( "<b>Paragraph.</b>" ).replaceAll( "p" ). Every <p> on the page is removed and a bold replacement appears in each location — three paragraphs become three bold elements.

📝 Syntax

jQuery .replaceAll() has one form:

Replace targets with matched elements — since 1.2

jQuery
.replaceAll( target )
  • target — selector, DOM element, array of elements, or jQuery object to replace.
  • Returns — the replacement jQuery collection.
  • Removed targets lose associated data and event handlers.

Official replaceAll pattern

jQuery
$( "<b>Paragraph.</b>" ).replaceAll( "p" );

Reversed replaceWith equivalent

jQuery
// These produce the same result:
$( "p" ).replaceWith( "<b>Paragraph.</b>" );
$( "<b>Paragraph.</b>" ).replaceAll( "p" );

⚡ Quick Reference

GoalCode
Replace all targets with new HTML$("<strong>New</strong>").replaceAll(".old")
Move existing element to replace target$("#source").replaceAll("#target")
Replace by selector string$(content).replaceAll("p")
Target-first alternative$("p").replaceWith(content)
Replace inner HTML only$("#box").html(content)
Chain after replacement$(content).replaceAll("p").addClass("done")

📋 .replaceAll() vs .replaceWith() vs .html() vs .remove()

Four DOM change APIs — know what each one actually replaces.

.replaceAll()
content first

Replacement collection replaces each target — selector names what to remove

.replaceWith()
target first

Matched elements are removed and swapped for new content — reversed syntax

.html()
inner only

Replace children inside the element — outer tag remains in the DOM

.remove()
delete

Remove matched elements with no replacement — pair with .append() or .before()

Examples Gallery

Example 1 follows the official jQuery API documentation. Examples 2–5 cover replacing with new HTML, moving an existing node, swapping by class, and upgrading deprecated buttons. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demo and new HTML replacements.

Example 1 — Official Demo: Replace All Paragraphs

Replace every <p> with a bold element containing the text “Paragraph.”

jQuery
$( "<b>Paragraph.</b>" ).replaceAll( "p" );

// Before: <p>Hello</p> <p>cruel</p> <p>World</p>
// After:  <b>Paragraph.</b> (×3 — one per replaced p)
Try It Yourself

How It Works

jQuery creates the replacement from the HTML string, then swaps it in for every element matched by the "p" selector. Each target is fully removed from the document tree.

Example 2 — Replace Inner Spans with a New Heading

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

jQuery
$( "<h2>New heading</h2>" ).replaceAll( ".inner" );

// DOM before:
// <div><span class="inner">Hello</span></div>
// <div><span class="inner">And</span></div>
// <div><span class="inner">Goodbye</span></div>

// DOM after: three <h2>New heading</h2> elements
Try It Yourself

How It Works

When multiple targets share the same replacement HTML string, jQuery inserts a copy at each location. The new heading replaces the span entirely — not just its text content.

📈 Move & Practical Patterns

Moving existing nodes and real-world upgrade workflows.

Example 3 — Move an Existing Element (Not Clone)

Select an element already on the page and use it to replace a target — the node moves from its old location.

jQuery
$( ".first" ).replaceAll( ".third" );

// Before: ... Hello (first) ... Goodbye (third)
// After:  ... Goodbye slot now contains Hello
// The .first element is MOVED — not duplicated
Try It Yourself

How It Works

When one existing DOM node replaces one target, jQuery detaches it from the old parent and inserts it at the target location. Event handlers on the moving node survive; handlers on the removed target are cleared.

Example 4 — Swap Placeholder with Live Content

Replace a loading placeholder with content that already exists elsewhere on the page.

jQuery
// HTML: #real-content hidden in sidebar, .placeholder in main area

$( "#real-content" ).replaceAll( ".placeholder" );

// Placeholder removed; real content now in main area
// #real-content moved from sidebar — one node, one target
Try It Yourself

How It Works

.replaceAll() is ideal when the replacement already exists in the DOM. You avoid duplicating markup and let jQuery handle the move in one step.

Example 5 — Upgrade Deprecated Buttons

Replace old .btn-old elements with accessible button markup site-wide.

jQuery
$( ".btn-old" ).each(function() {
  var label = $( this ).text();
  $( "<button type='button' class='btn-new'></button>" )
    .text( label )
    .replaceAll( this );
} );

// Each .btn-old replaced by a proper <button> with same label
// Old nodes and their handlers are removed
Try It Yourself

How It Works

Loop with .each() when each replacement needs unique text or attributes. Pass this (the current DOM element) as the target to replace one old button at a time.

🚀 Common Use Cases

  • Markup upgrades — swap deprecated tags for semantic HTML5 elements.
  • Template promotion — move hidden template content into visible slots.
  • Batch replacement — replace every matching node with standardized content.
  • DOM restructuring — move an existing widget to replace a placeholder.
  • Content migration — replace legacy class-based components with new ones.
  • Progressive enhancement — replace noscript fallbacks with interactive UI.

🧠 How .replaceAll() Swaps Elements

1

Build replacement collection

HTML string, existing selector, or jQuery object becomes the new content.

Content
2

Resolve target elements

Selector or jQuery object finds every node to remove from the DOM.

Target
3

Swap in replacement

Each target removed; replacement moved or cloned into position. Target data/events cleared.

Swap
4

Return replacement collection

The inserted elements returned for chaining — e.g. add classes or bind events.

📝 Notes

  • Available since jQuery 1.2.
  • Content-first syntax — reversed from .replaceWith().
  • Removed target nodes lose jQuery data and event handlers.
  • Single replacement into single target: element is moved, not cloned.
  • Multiple targets may clone the replacement after the first insertion.
  • Re-bind events on new elements after replacement if the old targets had handlers.
  • Not the same as JavaScript String.prototype.replaceAll() for text strings.

Browser Support

.replaceAll() has been part of jQuery since 1.2+. It uses standard DOM insertion and removal APIs through jQuery's manipulation layer. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.

jQuery 1.2+

jQuery .replaceAll()

Universal DOM replacement API across jQuery 1.x, 2.x, 3.x, and 4.x. Pair with .replaceWith() when target-first syntax reads more clearly.

100% With jQuery loaded
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() Universal

Bottom line: Default choice when replacement content is built first and you know the selector for targets to swap out.

Conclusion

The jQuery .replaceAll() method replaces target elements with the matched collection. Build or select the new content first, pass a selector or jQuery object as the target, and jQuery removes each old node while inserting the replacement — exactly as the official jQuery demo demonstrates.

Remember that replaced targets lose their data and event handlers, and that existing DOM nodes are moved rather than cloned when one element replaces one target. Next up: the target-first counterpart .replaceWith().

💡 Best Practices

✅ Do

  • Use content-first .replaceAll() when the replacement is already selected
  • Pass this inside .each() to replace one target at a time with custom content
  • Re-bind events on new elements after batch replacements
  • Prefer .replaceWith() when starting from the elements to remove
  • Use .detach() first if you need to preserve target data before swapping

❌ Don’t

  • Confuse jQuery .replaceAll() with JavaScript string replaceAll()
  • Assume replaced targets keep event handlers — they are removed with the node
  • Use .replaceAll() when you only need to change inner HTML — use .html()
  • Expect a moved node to remain in its original parent after single-target replace
  • Replace nodes without re-testing accessibility and keyboard focus order

Key Takeaways

Knowledge Unlocked

Six things to remember about .replaceAll()

The content-first API for swapping DOM elements.

6
Core concepts
02

Full swap

Outer node

Replace
03

Move

Not clone

DOM
04

vs replaceWith

Pair API

Compare
🧹 05

Cleans up

Targets

Data
1.2 06

Since 1.2

Core API

Stable

❓ Frequently Asked Questions

.replaceAll(target) replaces each element matched by the target selector with the elements in the current jQuery collection. The replacement content comes first in the syntax — like .insertBefore() vs .before(). Returns the inserted replacement elements. Available since jQuery 1.2.
.replaceWith(newContent) is called on the elements to remove: $('p').replaceWith('<b>Hi</b>'). .replaceAll(target) is called on the replacement content: $('<b>Hi</b>').replaceAll('p'). Same DOM result, reversed syntax — content-first vs target-first.
When one replacement element replaces one target, jQuery moves the element from its old location — it is not cloned. When the replacement set must appear in multiple target locations, jQuery clones for every target after the first.
The .replaceAll() method removes all data and event handlers associated with the target nodes being replaced. The replacement elements keep their own handlers. Plan teardown on targets before replacing if you need custom cleanup.
A CSS selector string, DOM element, array of DOM elements, or jQuery object identifying which element(s) to replace. Each matched target is swapped out for the replacement content.
.html(newContent) replaces the inner contents of matched elements — the outer tag stays. .replaceAll() removes the entire target element from the DOM and puts the replacement in its place. Use html() to change what is inside a div; use replaceAll() to swap the div itself.
Did you know?

JavaScript also has String.prototype.replaceAll() for replacing text in strings — completely unrelated to jQuery’s DOM method despite the same name. In jQuery, .replaceAll() follows the same content-first pattern as .insertBefore(), .insertAfter(), .appendTo(), and .prependTo(). When one existing element replaces one target, jQuery moves the node; when the same replacement must appear in multiple locations, clones are created for every target after the first.

Next: jQuery .replaceWith() Method

Replace matched elements with new content using target-first syntax.

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

6 people found this page helpful