The replaceChild()
method replaces a child node
with a new node.
node.replaceChild(newnode, oldnode)
Parameter | Description |
newnode |
Required. The node to insert. |
oldnode |
Required. The node to remove. |
Type | Description |
Node | The replaced node. |
The removeChild()
method removes an element's
child.
element.removeChild(node)
or
node.removeChild(node)
Parameter | Description |
node |
Required. The node (element) to remove. |
Type | Description |
Node |
The removed node (element).null if the child does not exist.
|
<!DOCTYPE html>
<html>
<head>
<title>DOM Navigation</title>
</head>
<body>
<ul id= "list">
<li>orange</li>
<li>Apple</li>
<li>Grapes</li>
<li>Banana</li>
</ul>
<script src="js/dom-create.js"></script>
</body>
</html>
/*JavaScript ReplaceChild*/
/*var newElement = document.createElement("li");
var newText = document.createTextNode("WOW new Text");
newElement.appendChild(newText);
var target = document.getElementById("list");
var oldElement = target.children[0];*/
//console.log(oldElement);
//target.replaceChild(newElement,oldElement);
/*JavaScript RemoveChild*/
var target = document.getElementById("list");
var oldElement = target.children[1];