+ 1

Code error

See my code . I'm trying to change the css of paragraph tag by using js . https://code.sololearn.com/WK4nghY4Cn8H/?ref=app

30th Jun 2019, 4:39 PM
Adnan Ansari
Adnan Ansari - avatar
3 ответов
+ 5
Adnan Ansari You can avoid this by nesting your all code in onload function onload=function() { var ad = document.getElementById("ad"); ad.style.color ="red"; } The error occurs because your js runs before loading of page. How will you set css properties of page if it doesn't exist! onload will run script when page loads and it'll work good 👌 😊 One more way to solve this problem is using <script> </script> tag 😊 all your js goes inside this tag and this tag should be after body tag to make sure page loods before script Thank you 😄
30th Jun 2019, 4:45 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
Remember to wrap the ode with window.onload when you are dealing with DOM Corrected code window.onload = () => { var ad = document.getElementById("ad") ad.style.color ="red"; } https://www.sololearn.com/post/90825/?ref=app
30th Jun 2019, 4:44 PM
Seniru
Seniru - avatar
+ 1
Thanks for your help
30th Jun 2019, 4:53 PM
Adnan Ansari
Adnan Ansari - avatar