so I'm learning about manipulating Dom elements and adding nodes.
I use this js code:
var parent = document.getElementById('div2'); var newNode = document.createElement('p'); var text = document.createTextNode('Text Node!'); newNode.appendChild(text); parent.insertBefore(newNode, parent.childNodes[4]); This is my html within the usual vanilla setup:
<div id="div2"> <p> abc </p> <p> cde </p> <p> efg </p> <p> ghi </p> </div> The output is this:
abc cde Text Node! efg ghi Isn't this the wrong spot? Isn't this insertBefore childNode[2] before it's inserted? For some reason, when I change the number after childNode, the text is not showing up where I expect it to be? Why is that? Aren't the nodes still starting at 0 ... like arrays? Another example, using childNode[6] places it after efg. I thought that should have been insertBefore childNode[3].
I know the question's probably basic but I can't figure out why. thanks!