+ 1

Please help

Not working . Style. <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title> fade in out </title> <script type="text/javascript"> var elem = document.getElementById("img"); var duration = 1000; var thide = 1000; var tshow = 1000; function setOpacity(value){ elem.style.opacity = value; } function fadeOut(){ for ( i = 0; i <= 1; i+=0.01){ setTimeout("setOpacity("+(1-i)+")",i*duration); } setTimeout("fadeIn()",(duration+thide)); } function fadeIn(){ for ( i = 0; i <= 1; i+=0.01){ setTimeout("setOpacity("+i+")",i*duration); } setTimeout("fadeOut()",(duration+tshow)); } </script> </head> <body> <button onclick="fadeOut()">Start</button> <img id="img" src="bulbon.gif"/> </body> </html>

21st Apr 2020, 7:24 AM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
2 Réponses
+ 3
Don't write var elem = document.getElementById("img) outside the function because in this case JavaScript load first before loading rest part so you will get null and you know that you can't do operation on null.
21st Apr 2020, 7:33 AM
A͢J
A͢J - avatar
+ 2
Thanks a lot. A J
21st Apr 2020, 8:50 AM
Md Iftakher Hossain
Md Iftakher Hossain - avatar