+ 1

Greetings, Coders please let me know that what the error here in JS

Greetings, I want to know the problem and solution of this code why I can't access the HTML elements with JS. I'm trying to do this but everytime I get null value. Thank you in advance. https://code.sololearn.com/W3CP5rKdqegz/?ref=app Thank you

16th Jun 2022, 11:03 AM
Adarsh Addee
Adarsh Addee - avatar
9 Answers
+ 5
In sololearn, Put js code in window.onload = () => { // js code here... }
16th Jun 2022, 11:14 AM
Jayakrishna 🇮🇳
+ 2
Use onload function Try this: onload = () => { console.log(demo) console.log(demo.innerHTML) }
16th Jun 2022, 12:31 PM
SAN
SAN - avatar
+ 1
onload = function() { var demo = document.getElementById("demo").innerText; let demo2 = document.querySelector("#demo").innerText; console.log(demo2) console.log(demo) }
16th Jun 2022, 1:13 PM
Calviղ
Calviղ - avatar
+ 1
Thank you everyone 🥺
16th Jun 2022, 4:12 PM
Adarsh Addee
Adarsh Addee - avatar
+ 1
document.querySelector("#demo").style.color = "pink";
16th Jun 2022, 10:43 PM
Calviղ
Calviղ - avatar
+ 1
Thank you but it's getting error please check
17th Jun 2022, 6:11 AM
Adarsh Addee
Adarsh Addee - avatar
+ 1
Use this document.querySelector("#demo").style.color = "pink";
17th Jun 2022, 8:46 AM
Calviղ
Calviղ - avatar
+ 1
Thank you it worked 😋
18th Jun 2022, 12:48 AM
Adarsh Addee
Adarsh Addee - avatar
0
Kindly let me know how can I add style in html via js code.
16th Jun 2022, 4:12 PM
Adarsh Addee
Adarsh Addee - avatar