JavaScriptDOM and events

Element.replaceWith()

Replaces an element in the document with other elements or text. The old element is removed from the page.

Returns
undefined
Changes the original
Yes
On this page

Example

#
JavaScript
const buyButton = document.querySelector('#buy')

const badge = document.createElement('span')
badge.className = 'badge'
badge.textContent = 'Purchased ✓'

buyButton.replaceWith(badge)

console.log(buyButton.isConnected)
console.log(document.querySelector('.card').lastElementChild)
console.log(document.querySelector('#buy'))
Console
false
<span class="badge">
null

Definition and usage

#

The replaceWith() method inserts the given nodes or strings in place of an element, and removes the element itself from the document. The new content lands exactly where the old element was: it gets the same parent and the same siblings.

It accepts any number of arguments, so you can replace one element with several. Strings are inserted as plain text, not as HTML, just like in append(). If you pass an element that’s already on the page, it gets moved to the new spot.

The replaced element doesn’t disappear from memory. As long as you hold a reference to it in a variable, you can restore it later, for example with another replaceWith() call. The older way to do the same thing is parent.replaceChild(new, old), which requires a reference to the parent and takes its arguments in a less intuitive order.

Syntax

#
Syntax
element.replaceWith(node1, node2, …)
element.replaceWith(text)

Parameters

#
  • node1…nodeN

    elements or strings

    Nodes or strings that will take the element’s place, in the given order.

More examples

#
Several nodes instead of one
JavaScript
const status = document.querySelector('#status')
const points = document.createElement('strong')
points.textContent = '340 pts'

status.replaceWith('7 lessons complete, ', points)

const paragraph = document.querySelector('p')
console.log(paragraph.textContent)
console.log(paragraph.innerHTML)
Console
Status: 7 lessons complete, 340 pts
Status: 7 lessons complete, <strong>340 pts</strong>
Edit mode: text swapped for a field and back
JavaScript
const title = document.querySelector('#title')
const input = document.createElement('input')
input.value = title.textContent

title.replaceWith(input)
console.log(document.querySelector('#title'))

input.value = 'A note about loops'
title.textContent = input.value
input.replaceWith(title)

console.log(document.querySelector('#title').textContent)
Console
null
A note about loops

After being replaced, the heading isn’t in the document, but the title variable still holds it. That’s why it returns to its spot with new text after the edit.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • You call replaceWith() on the element that should disappear. You don’t need a reference to the parent, unlike with replaceChild().
  • Strings are inserted as text. If the new content should be HTML code, build elements with createElement(), or insert the code with insertAdjacentHTML() and only then remove the old element.
  • Keep a reference to the old element in a variable if you’ll need to restore it, for example after leaving edit mode.

Related entries

#

Spotted a mistake or missing an example? Write to us.