Definition and Usage..
The replaceChild() method replaces a child node with a new node.
The new node could be an existing node in the document, or you can create a new node.
Tip: Use the removeChild() method to remove a child node from an element.
Syntax: node.replaceChild(newnode, oldnode)
Different way to how to declare replaceChild() in program.
CODING:The replaceChild() method replaces a child node with a new node.
The new node could be an existing node in the document, or you can create a new node.
Tip: Use the removeChild() method to remove a child node from an element.
Syntax: node.replaceChild(newnode, oldnode)
Different way to how to declare replaceChild() in program.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace the first item in the the list.</p>
<ul id="List1"><li>XHTML</li><li>CSS</li><li>JAVA SCRIPT</li></ul>
<button onclick="replaceElement()">Replace Element</button>
<ul id="List2"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>
<button onclick="replacetext()">Replace Text</button>
<script>
function replaceElement() {
var elmnt = document.createElement("h3");
var textnode = document.createTextNode("Languages");
elmnt.appendChild(textnode);//also define,elmnt.innerHTML="";
var item = document.getElementById("List1");
item.replaceChild(elmnt, item.childNodes[0]);
}
function replacetext() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("List2").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
//also define,var item = document.getElementById("List2").childNodes[0].innerHTML="fd";
}
</script>
</body>
</html>
<html>
<body>
<p>Click the button to replace the first item in the the list.</p>
<ul id="List1"><li>XHTML</li><li>CSS</li><li>JAVA SCRIPT</li></ul>
<button onclick="replaceElement()">Replace Element</button>
<ul id="List2"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>
<button onclick="replacetext()">Replace Text</button>
<script>
function replaceElement() {
var elmnt = document.createElement("h3");
var textnode = document.createTextNode("Languages");
elmnt.appendChild(textnode);//also define,elmnt.innerHTML="";
var item = document.getElementById("List1");
item.replaceChild(elmnt, item.childNodes[0]);
}
function replacetext() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("List2").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
//also define,var item = document.getElementById("List2").childNodes[0].innerHTML="fd";
}
</script>
</body>
</html>
Comments
Post a Comment