Want to replace a child node with a new node in JavaScript? XML DOM replaceChild() Method 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: The replaced child node can be inserted later into any element in the same document.
1 2 3 4 5 6 7 8 9 10 | <html> <body> <ul id="List"><li>Boost</li><li>Horlicks</li><li>Maltova</li></ul> <script> var newnode = document.createTextNode("Bournavita"); var item = document.getElementById("List").childNodes[0]; item.replaceChild(newnode, item.childNodes[0]); </script> </body> </html> |
Output
1 2 3 | Bournavita Horlicks Maltova |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.