I'm having an issue with the code snippet from the W3Schools website. When I use the LI element in a straight line, it works fine. But when I try to format it differently, it doesn't work. Any ideas on what could be causing this?
This is the code that is not working:
<ul id="myList">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<p>Click the button to replace the first item in the list.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("myList").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
}
</script>
However, this code version does work:
<ul id="myList"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>
<p>Click the button to replace the first item in the list.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var textnode = document.createTextNode("Water");
var item = document.getElementById("myList").childNodes[0];
item.replaceChild(textnode, item.childNodes[0]);
}
</script>