1 // select the element that will be replaced
2 var el = document.querySelector('div');
3
4 // <a href="/javascript/manipulation/creating-a-dom-element-51/">create a new element</a> that will take the place of "el"
5 var newEl = document.createElement('p');
6 newEl.innerHTML = '<b>Hello World!</b>';
7
8 // replace el with newEL
9 el.parentNode.replaceChild(newEl, el);
1// select the element that will be replacedvar el = document.querySelector('div');
2// <a href="/javascript/manipulation/creating-a-dom-element-51/">create a new element</a> that will take the place of "el"var newEl = document.createElement('p');newEl.innerHTML = '<b>Hello World!</b>';
3// replace el with newELel.parentNode.replaceChild(newEl, el);
1<html>
2<head>
3</head>
4<body>
5 <div>
6 <a id="myAnchor" href="http://www.stackoverflow.com">StackOverflow</a>
7 </div>
8<script type="text/JavaScript">
9 var myAnchor = document.getElementById("myAnchor");
10 var mySpan = document.createElement("span");
11 mySpan.innerHTML = "replaced anchor!";
12 myAnchor.parentNode.replaceChild(mySpan, myAnchor);
13</script>
14</body>
15</html>
16