Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The replaceChild() Method</h2> <ul id="myList"> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ul> <p>Click the button to replace the first item in the the list.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { const element = document.createElement("li"); const textNode = document.createTextNode("Water"); element.appendChild(textNode); const list = document.getElementById("myList"); list.replaceChild(element, list.children[0]); } </script> </body> </html>