The replaceWith() method in Javascript
The replaceWith()
method allows you to replace an existing element with another element or text, completely removing the old node from the `DOM'.
element.replaceWith(new Element);
Here, a new Element
is a new element, text, or several nodes that will replace the old element.
The replacement example
let old Item = document.getElementById('old');
let new Item = document.createElement('div');
newItem.textContent = 'I'm a new element!';
oldItem.replaceWith(newItem);
An example of replacing an element with text
document.getElementById('title').replaceWith('There was a Cxd3');
The replaceWith()
method is a tool for replacing elements without changing the parent structure. It is ideal when you need to replace a component without removing or modifying other elements.