I know this is an easy concept, but I just can't seem to get it working. Any ideas why? Here is my code:
var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";
oldChild.replaceChild(newChild, oldChild);
I know this is an easy concept, but I just can't seem to get it working. Any ideas why? Here is my code:
var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";
oldChild.replaceChild(newChild, oldChild);
Share
Improve this question
asked Jun 12, 2012 at 0:28
DanielDaniel
4,34212 gold badges51 silver badges69 bronze badges
1 Answer
Reset to default 9You're calling replaceChild()
on oldchild
, when you ought to be calling it on the parent node of oldchild
var oldChild = document.getElementById("sbX1");
var newChild = document.createElement("div");
newChild.id= "sbYY1";
// Replace oldchild on the parent node
// You can reference the child's parent via .parentNode
// or retrieve it directly with document.getElementById('theparentId')
oldchild.parentNode.replaceChild(newChild, oldChild);