This is a follow up to this question:
How to replace DOM element in place using Javascript?
I wasn't the OP but I am facing a similar situation. The initial question was "how to replace an anchor
element with a span
using Javascript. The answer that was given (thank you Bjorn Tipling ) was to use replaceChild()
, with the following example:
<html>
<head>
</head>
<body>
<div>
<a id="myAnchor" href="http://www.stackoverflow">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>
My follow up is: how to add / insert an id (e.g., "xyz"
)and a function (e.g., onmouseout='doWhatever(this)'
) to the replaced DOM element?