+ 3

Doubt regarding DOM

HTML: <html> <body> <div id ="demo"> <p>some text</p> <p>some other text</p> </div> </body> </html> JS: function setText(){ var a = document.getElementById("demo"); var b = a.childNodes; b[0].innerHTML = "new text"; } //calling the function with setTimeout to make sure the HTML is loaded setTimeout(setText, 500); It doesnt work!Plz explain

30th Jun 2017, 12:36 PM
Calden Rodrigues
Calden Rodrigues - avatar
11 odpowiedzi
+ 13
"a.ChildNodes" should be "a.childNodes" (mind the case...)
30th Jun 2017, 12:33 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 11
onload=SetText; ...... //Yes, try this.....
30th Jun 2017, 1:13 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 10
This code working for me: <html> <body> <div id ="demo"> <p>some text</p> <p>some other text</p> </div> <script> function setText(){ var a = document.getElementById("demo"); var b = a.querySelectorAll("*"); b[0].innerHTML = "new text"; } //calling the function with setTimeout to make sure the HTML is loaded setText (); </script> </body> </html> https://code.sololearn.com/Wb4SBvM6j3WZ/?ref=app
30th Jun 2017, 1:28 PM
Immanuel
Immanuel - avatar
+ 10
use "a.querySelectorAll("*")" instead of "a.childNodes"
30th Jun 2017, 1:36 PM
Immanuel
Immanuel - avatar
+ 3
use the .children property like a.children
30th Jun 2017, 1:15 PM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 3
You have 5 childNode there. b[1].innerHTML would update first paragraph.
30th Jun 2017, 1:27 PM
Calviղ
Calviղ - avatar
+ 3
use this one its working I tested it b[1].innerHTML="new text"; windows.onload= function() { setText(); };
30th Jun 2017, 1:31 PM
Peshkawt Mahmood
Peshkawt Mahmood - avatar
+ 2
Don't use timeout to check DOM loading, use window.online Just add window.onload = setText;
30th Jun 2017, 12:34 PM
Calviղ
Calviղ - avatar
+ 1
I realized that, changed it still does not work.
30th Jun 2017, 12:35 PM
Calden Rodrigues
Calden Rodrigues - avatar
+ 1
Still no good. damm!!!
30th Jun 2017, 12:38 PM
Calden Rodrigues
Calden Rodrigues - avatar
+ 1
change the 0 to 1 index b[1].innerHTML="new text"; windows.onload= function() { setText(); };
30th Jun 2017, 1:11 PM
Peshkawt Mahmood
Peshkawt Mahmood - avatar