Replace a DOM element

Remove an element from the DOM tree and insert a new one in its place.


Replacing one element with another by use of the cross-browser safe DOM method replaceChild():

// select the element that will be replaced
var el = document.querySelector('div');

// create a new element that will take the place of "el"
var newEl = document.createElement('p');
newEl.innerHTML = '<b>Hello World!</b>';

// replace el with newEL
el.parentNode.replaceChild(newEl, el);

Find other examples about the replaceChild() method on w3schools.


Feedback