+ 1

DOM selector help

Am I using the selector wrong? In HTML. <p id = "demo"> test <p> In JavaScript. El = document.getElementById("demo"); But if I do alert (El.innerHTML) I get an error null, while it worked at www3schools https://code.sololearn.com/WNJrsk940ULv/?ref=app

17th Jan 2022, 11:13 PM
Arnold Nijmeijer
10 Answers
+ 2
Or replace lines 21-22 with: onload=()=>demo.innerText = textje;
18th Jan 2022, 12:23 AM
Solo
Solo - avatar
+ 3
put your code inside window.onload =function(){ //your code }
17th Jan 2022, 11:49 PM
Bahha┣
Bahha┣ - avatar
+ 2
Because js loads faster than html, and therefore does not see "demo"
18th Jan 2022, 1:53 AM
Solo
Solo - avatar
+ 1
onclick works, it's just that count reached 15.
18th Jan 2022, 3:41 AM
Solo
Solo - avatar
+ 1
You need to zero out all variables at the end of the "output" function: count=countFunction=0; check=[]; And now remove onload.
21st Jan 2022, 11:55 PM
Solo
Solo - avatar
+ 1
To avoid confusion, initialize these variables on the first line with let and they will still be visible in all functions. And once again: "onload is not needed since you are accessing the html tag after the html document is fully loaded using a function call on click".
22nd Jan 2022, 3:02 AM
Solo
Solo - avatar
0
Now the onclick doesn't work, prolly need also onload?
18th Jan 2022, 1:49 AM
Arnold Nijmeijer
0
Dooh , thanks!
18th Jan 2022, 11:18 PM
Arnold Nijmeijer
0
Where do I put count = 0 when I click button,
21st Jan 2022, 11:11 PM
Arnold Nijmeijer
0
Yes , thanks but I needed a onload the first time , so if you use global variable in a function they are the same in the 2nd function? If you call the second from the first?
22nd Jan 2022, 1:51 AM
Arnold Nijmeijer