+ 1

Please can you tell me what is the problem of this code?why omar dosent change to ola?

https://code.sololearn.com/WiflyWEXUftD/?ref=app

15th Jun 2019, 6:35 PM
Omar
Omar - avatar
12 ответов
+ 2
It's because you defined the variabld before the page loaded, therefore it will be null. window.onload = () => { document.getElementById(1) = "oma"; } Also note that using numbers for ids is a bad practice
15th Jun 2019, 6:40 PM
Airree
Airree - avatar
+ 1
Which var i defined?!! And also i worte what u say in the code and i still have the problem....you can see the code
15th Jun 2019, 6:47 PM
Omar
Omar - avatar
+ 1
Did you forget to write innerHTML ?
15th Jun 2019, 6:48 PM
Omar
Omar - avatar
+ 1
Yes, I did, but you forgot to write the arrow sign :/ (=>)
15th Jun 2019, 7:04 PM
Airree
Airree - avatar
+ 1
Ok thanks☺
15th Jun 2019, 7:06 PM
Omar
Omar - avatar
+ 1
Airree you said that my code didnot run because i defined the variable before the page loades Which variable ?
15th Jun 2019, 9:11 PM
Omar
Omar - avatar
+ 1
I guess I wasn't really paying attention. I just meant that you tried to get an element which didn't exist yet
15th Jun 2019, 9:12 PM
Airree
Airree - avatar
+ 1
How it didnot exist yet?! I wrote it in html body ??
15th Jun 2019, 9:13 PM
Omar
Omar - avatar
+ 1
No, it just means it didn't exist at the time you ran the function, because the page wasn't loaded.That's why you should use the window.onload function, which will only run when the document is loaded
15th Jun 2019, 9:15 PM
Airree
Airree - avatar
+ 1
You mean that when i click "run" .... js codes is runs first...then html ?
15th Jun 2019, 9:17 PM
Omar
Omar - avatar
+ 1
Yes, kind of
15th Jun 2019, 9:17 PM
Airree
Airree - avatar
+ 1
Ok thanks alot
15th Jun 2019, 9:18 PM
Omar
Omar - avatar