+ 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
3 Answers
+ 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 š
+ 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
+ 1
Thanks for your help