I ran into a weird issue I can't seem to figure out. I have this basic JS code
var myList = document.getElementById("my-list") myList.innerHTML += "<li>3</li>" document.body.innerHTML += "<p id='paragraph'>V School rocks!</p>" document.getElementById("paragraph").style.textAlign = "center" var a = document.createElement("li") a.textContent = "element" console.log(myList) myList.append(a) and this html
<!DOCTYPE html> <html> <head> </head> <body> <html> <head> </head> <body> <ul id="my-list"> <li>0</li> <li>1</li> <li>2</li> </ul> <script src="demo2.js"></script> </body> </html> </body> </html> the last line of JS code does not append my element into the list although if I console log the list it shows the element as existing there.
In order to make this code work properly, I have make one of the following updates:
- remove the 4th and 5th lines of code.
- redeclare the myList variable below the 4th and 5th lines of code.
Any idea why this happens?
Thanks in advance