MDN wants to learn about developers like you:

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The ChildNode.replaceWith() method replaces this ChildNode in the children list of its parent with a set of Node or DOMString objects. DOMString objects are inserted as equivalent Text nodes.


[Throws, Unscopable] 
void ChildNode.replaceWith((Node or DOMString)... nodes);


A set of Node or DOMString objects to replace.



Using replaceWith()

var parent = document.createElement("div");
var child = document.createElement("p");
var span = document.createElement("span");


// "<div><span></span></div>"

ChildNode.replaceWith() is unscopable

The replaceWith() method is not scoped into the with statement. See Symbol.unscopables for more information.

with(node) { 
// ReferenceError: replaceWith is not defined 


You can polyfill the replaceWith() method in Internet Explorer 6 and higher with the following code:

function ReplaceWith(Ele) {
  'use-strict'; // For safari, and IE > 10
  var parent = this.parentNode,
      i = arguments.length,
      firstIsNode = +(parent && typeof Ele === 'object');
  if (!parent) return;
  while (i-- > firstIsNode){
    if (parent && typeof arguments[i] !== 'object'){
      arguments[i] = document.createTextNode(arguments[i]);
    } if (!parent && arguments[i].parentNode){
    parent.insertBefore(this.previousSibling, arguments[i]);  }
  if (firstIsNode) parent.replaceChild(this, Ele);
if (!Element.prototype.replaceWith)
    Element.prototype.replaceWith = ReplaceWith;
if (!CharacterData.prototype.replaceWith)
    CharacterData.prototype.replaceWith = ReplaceWith;
if (!DocumentType.prototype.replaceWith) 
    CharacterData.prototype.replaceWith = ReplaceWith;


Specification Status Comment
The definition of 'ChildNode.replacewith()' in that specification.
Living Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support 54.0 49 (49) No support[1] No support 39.0 No support
Feature Android Webview Firefox Mobile (Gecko) IE Mobile Edge Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 49.0 (49) No support ? 39.0 No support 54.0

[1] Internet Explorer supports the non-standard HTMLElement.replaceNode method; see MSDN: replaceNode

See also

Document Tags and Contributors

 Contributors to this page: anonyco, stevenwdv, jszhou, jpmedley, fscholz
 Last updated by: anonyco,