+ 11

DOM not working. Why this error ?

I'm trying to change the color on user click using global variable but I'm not sure where im doing wrong 😥 https://code.sololearn.com/WqcX2Xlrgwjx/?ref=app

23rd Oct 2020, 9:14 AM
Jeffly
Jeffly - avatar
25 Réponses
+ 6
DOM cant access your element because the script tag is automatically added on the head tag on sololearn. the DOM would create the structure from top to bottom so when it reached the script tag first it would run your script but wait, you already wanted to access the DOM object b4 it even reached the body tag. causing it to reach an exception error. so one of the solutions is to put the script tag under body so that the DOM structure has been established before accessing it. but no professional actually does that. what you should do is to use the window.onload method to run the function only after DOM finished parsing the structure of your HTML so your js should be window.onload = start var fo; //global variable function start(){ fo=document.getElementById("tst"); } function test(){ fo.style.color="blue"; }
24th Oct 2020, 10:21 PM
Shen Bapiro
Shen Bapiro - avatar
+ 23
Use Script tag and define whole Js in that and see,It's working I edit and try https://code.sololearn.com/Wr7YapZPD6Ey/?ref=app
23rd Oct 2020, 9:41 AM
Prachi The Pari ⭐
Prachi The Pari ⭐ - avatar
+ 22
ÃKR Abhay also Right✓ Use all your variable inside the Function and it's working
23rd Oct 2020, 9:51 AM
Prachi The Pari ⭐
Prachi The Pari ⭐ - avatar
+ 12
Do function test(){ tst.style.color="blue"; }
23rd Oct 2020, 9:22 AM
ÃKR
ÃKR - avatar
+ 6
https://code.sololearn.com/W5ewdKzJ7oIE/?ref=app if you don't want to write js in html file ,you can pass id value as Argument to function and then select it inside function. Something like this #in html file onclick='test("tst")' #in js file function test(tst){ var f=document.getElementById(tst); f.style.color="blue"; } Edit: since id is unique it is available globally and so instead of selecting it through document . getElement... You can directly refer the element by its id name like ÃKR did but it's not a good idea as it's not clear enough where that tst came from and also if attributes and methods of window object and other objects loaded from external scripts have same name ,it won't work
23rd Oct 2020, 9:41 AM
Abhay
Abhay - avatar
+ 6
Window.onload
23rd Oct 2020, 10:17 AM
Papa Penguin
+ 5
Faq
23rd Oct 2020, 9:48 AM
Kevin ★
+ 5
function test(){ var forus = document.querySelector("#tst"); forus.style.color = "green"; } IT WORKS AFTER
25th Oct 2020, 5:39 AM
Lazizbek
Lazizbek - avatar
+ 5
Variables must be included inthe function.
25th Oct 2020, 5:51 AM
Lazizbek
Lazizbek - avatar
+ 4
function test(){ var fo = document.getElementById("tst"); fo.style.color="blue"; } Try this way bro
23rd Oct 2020, 10:02 AM
Ayush Kumar
Ayush Kumar - avatar
+ 4
Do HTML = This code here ..............? It will start working again
24th Oct 2020, 5:50 PM
MOHAMMAD AZMAL ALI
MOHAMMAD AZMAL ALI - avatar
23rd Oct 2020, 9:16 AM
Jeffly
Jeffly - avatar
+ 3
KINGDX But I don't want to do that If I declare the var outside the Function then it could be used globally isn't it?
23rd Oct 2020, 9:29 AM
Jeffly
Jeffly - avatar
+ 3
Now write [Solved] before the question so others will know from outside that it's solved.😎
24th Oct 2020, 5:35 PM
Adil
Adil - avatar
+ 1
Declare your var inside the function : function test(){ var fo=document.getElementById("tst"); fo.style.color="blue"; }
23rd Oct 2020, 9:28 AM
CHMD
CHMD - avatar
+ 1
It is possible by defining the variable inside the function
23rd Oct 2020, 11:17 PM
Qutaba Qais Mahmood
Qutaba Qais  Mahmood - avatar
+ 1
//paste this in the JavaScript section document.addEventListener( "DOMContentLoaded", function(){ test() }); function test(){ var fo=document.getElementById("tst"); fo.style.color="blue"; }
24th Oct 2020, 7:32 AM
Atoms~⚛
Atoms~⚛ - avatar
+ 1
Atomic Wave This code automatically changes the color to blue, while in the HTML code the change is when clicking on the text
24th Oct 2020, 11:11 AM
Qutaba Qais Mahmood
Qutaba Qais  Mahmood - avatar
+ 1
Just put the variable inside the function.
24th Oct 2020, 4:55 PM
Adil
Adil - avatar
25th Oct 2020, 8:51 AM
Kirabo Ibrahim
Kirabo Ibrahim - avatar