+ 1

Why it does't work? arr[0].innerHTML

function setText() { var a = document.getElementById("demo"); var arr = a.childNodes; arr[0].innerHTML = "new text"; } setTimeout(setText, 500);

28th Jun 2019, 2:23 PM
Rowrowak 👤
Rowrowak 👤 - avatar
5 ответов
+ 2
make sure that arr[0] is the element you want to modify you can print arr[0].nodeName to find out
28th Jun 2019, 2:30 PM
Taste
Taste - avatar
+ 2
Taste Thanks for your quick response. <html> <body> <div id ="demo"> <p>some text</p> <p>some other text</p> </div> </body> </html> Just arr[1] outputs "P". Not arr[0]
28th Jun 2019, 2:40 PM
Rowrowak 👤
Rowrowak 👤 - avatar
+ 2
then arr[1] is the node that you want to modify, not 0. you can loop trough all the childNodes, then in each itteration check the type if its P (or other type of element you expect) then thats the element you re looking for.
28th Jun 2019, 2:43 PM
Taste
Taste - avatar
+ 2
you're welcome
28th Jun 2019, 2:51 PM
Taste
Taste - avatar
+ 1
Thank you Taste
28th Jun 2019, 2:48 PM
Rowrowak 👤
Rowrowak 👤 - avatar