+ 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
11 Respostas
+ 13
"a.ChildNodes" should be "a.childNodes"
(mind the case...)
+ 11
onload=SetText; ......
//Yes, try this.....
+ 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
+ 10
use "a.querySelectorAll("*")" instead of "a.childNodes"
+ 3
use the .children property like a.children
+ 3
You have 5 childNode there.
b[1].innerHTML would update first paragraph.
+ 3
use this one its working I tested it
b[1].innerHTML="new text";
windows.onload= function() {
setText();
};
+ 2
Don't use timeout to check DOM loading,
use window.online
Just add
window.onload = setText;
+ 1
I realized that, changed it still does not work.
+ 1
Still no good. damm!!!
+ 1
change the 0 to 1 index
b[1].innerHTML="new text";
windows.onload= function() {
setText();
};