Favorites

DOM Element.replaceWith() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.replaceWith()
DOM Element MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The replaceWith() method replaces an element in the page with other nodes or text.

Description

The replaceWith() method replaces the element with the given nodes or strings, in its parent's list of children. Strings are inserted as text.

If the element has no parent, the call does nothing. Like other insertion methods, nodes passed to it are moved if they're already in the page, and you can pass several nodes and strings to replace one element with many.

A common use is swapping a placeholder, such as a loading message or skeleton, for real content once data arrives. The element's own event listeners don't transfer to its replacement.

Syntax

element.replaceWith(param1, param2, ...)

Parameters

  • param1, param2, ...Nodes or strings to put in place of the element.

Return Value

None (undefined).

Example

<p id="loading">Loading...</p>
const loading = document.getElementById('loading');
const result = document.createElement('p');
result.textContent = 'Loaded 3 items.';
loading.replaceWith(result);
console.log(document.body.innerHTML.trim());

Browser Support

The following information will show you the current browser support for the Element.replaceWith() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee