+ 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>

15th May 2017, 10:04 AM
M. Iqbal Maulana
M. Iqbal Maulana - avatar
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"
15th May 2017, 10:13 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 4
yes sir!
15th May 2017, 10:24 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 1
oh i see, so first child is like [0]
15th May 2017, 10:23 AM
M. Iqbal Maulana
M. Iqbal Maulana - avatar
+ 1
thanks a lot :)
15th May 2017, 10:25 AM
M. Iqbal Maulana
M. Iqbal Maulana - avatar