+ 3
Why it can't work
i want to change 'some text' to 'another text' i use first child, but why it say firstChild of null, here my script <html> <body> <div id ="demo"> <p>some text</p> <p>some other text</p> </div> <script type="text/javascript"> var test = document.getElementById("demo"); var a = test.firstChild; a.innerHTML = "Another Text"; </script> </body> </html>
4 Answers
+ 6
try to remove .firstChild out.then it will worked well
you might ask with "why?"
bc document.getElementById will return/get HTMLElement(one element only)
while getElementsByTagName, getElementsByClassName or another selector
Will return/get Multiple elements so.... can have "firstChild"
+ 4
yes sir!
+ 1
oh i see, so first child is like [0]
+ 1
thanks a lot :)