Why does changing color by using js doesn't work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does changing color by using js doesn't work

Why is this js code doesn't work https://code.sololearn.com/W2BTjj3i2AS7/?ref=app Note: this is my first code so it's quite ugly

12th Sep 2021, 5:28 PM
Mohammad _io_
6 Answers
+ 3
Try to use window.onload=()=>{ var p=document.getElementById("p"); p.style.backgroundColor="blue"; }
12th Sep 2021, 5:30 PM
Pariket Thakur
Pariket Thakur - avatar
+ 1
Your HTML contens load before the JavaScript so you have to write JavaScript code inside the window.onload function so your Javascript code will execute after loading HTML contents. window.onload = function () { //Your code }
12th Sep 2021, 5:46 PM
Pariket Thakur
Pariket Thakur - avatar
0
Thanks
12th Sep 2021, 5:33 PM
Mohammad _io_
0
#HrCoder sorry but can you explain why?
12th Sep 2021, 5:42 PM
Mohammad _io_
0
Use @ instead of #
12th Sep 2021, 5:46 PM
Pariket Thakur
Pariket Thakur - avatar
0
Thank you HrCoder
12th Sep 2021, 5:47 PM
Mohammad _io_