JavaScript node.replaceChild() Method

You are Here:

JavaScript node.replaceChild() Method

The node.replaceChild() method replaces a child node with a new node.

Note: The new node could be an existing node in the document, or you can create a new node.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <ul><li>Honda</li><li>Yamaha</li></ul> <button onclick="myFunction()">Remove Child</button> <script> function myFunction(){ var y = document.getElementsByTagName("ul")[0].childNodes[0]; var oldNode = y.childNodes[0]; var newNode = document.createTextNode("Harley Davidson"); y.replaceChild(newNode, oldNode); } </script> </body> </html>

Syntax

node.replaceChild(newChild, oldChild)

Parameter Values

ValueTypeExplanation
newChildRequiredthe new node to replace oldChild.
oldChildRequiredthe existing child to be replaced..

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author