replace element in dom javascript code example
Example 1: javascript replace element
<html>
<head>
</head>
<body>
<div>
<a id="myAnchor" href="http://www.stackoverflow.com">StackOverflow</a>
</div>
<script type="text/JavaScript">
var myAnchor = document.getElementById("myAnchor");
var mySpan = document.createElement("span");
mySpan.innerHTML = "replaced anchor!";
myAnchor.parentNode.replaceChild(mySpan, myAnchor);
</script>
</body>
</html>
Example 2: javascript replace element
var el = document.querySelector('div');
var newEl = document.createElement('p');
newEl.innerHTML = '<b>Hello World!</b>';
el.parentNode.replaceChild(newEl, el);