javascript - Change background image by month -
i'm trying change background image of element, according season is. i'm getting following error:
typeerror: document.getelementsbyclassname(...).style undefined
var currenttime = new date(); var month = currenttime.getmonth() + 1; var total = month; // summer if (total >= 6 && total <= 8) { document.getelementsbyclassname("banner-container").style.backgroundimage = "url('images/homepage-banners/winter-banner.jpg')"; } // autumn else if (total >= 9 && total <= 11) { document.getelementsbyclassname("banner-container").style.backgroundimage = "url('images/homepage-banners/fall-banner.jpg')"; } // winter else if (total == 12 || total == 1 || total == 2) { document.getelementsbyclassname("banner-container").style.backgroundimage = "url('images/homepage-banners/winter-banner.jpg')"; } // spring else if (total >= 2 && total <= 6) { document.getelementsbyclassname("banner-container").style.backgroundimage = "url('images/homepage-banners/spring-banner.jpg')"; } else { document.getelementsbyclassname("banner-container").style.backgroundimage = "url('images/homepage-banners/summer-banner.jpg')"; }
updated script markup, switching getelementbyid:
<div class="custom banner-container"> <div id="home-banner"> <div class="dmr-welcome"> <img src="/dev/images/homepage-banners/dmr-banner1_07.jpg"> </div> </div> </div> var currenttime = new date(); var month = currenttime.getmonth() + 1; var total = month; // summer if (total >= 6 && total <= 8) { document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/winter-banner.jpg')"; } // autumn else if (total >= 9 && total <= 11) { document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/fall-banner.jpg')"; } // winter else if (total == 12 || total == 1 || total == 2) { document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/winter-banner.jpg')"; } // spring else if (total >= 2 && total <= 6) { document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/spring-banner.jpg')"; } else { document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/summer-banner.jpg')"; }
error is:
typeerror: document.getelementbyid(...) null document.getelementbyid("home-banner").style.backgroundimage = "url('images/homepage-banners/fall-banner.jpg')";
as noted elclanrs, getelementsbyclassname not you're looking for. looks me want getelementbyid.
also, javascript executing before has reference div you're trying set background image on. try putting script @ bottom of page.
the following worked me. note made changes file names , image path.
<!doctype html> <html> <head> <title></title> </head> <body> <div id="banner-container" style="width:400px;height:300px;"></div> <script> var currenttime = new date(); var month = currenttime.getmonth() + 1; var total = month; // summer if (total >= 6 && total <= 8) { document.getelementbyid("banner-container").style.backgroundimage = "url('images/winter.png')"; } // autumn else if (total >= 9 && total <= 11) { document.getelementbyid("banner-container").style.backgroundimage="url('images/fall.png')"; } // winter else if (total == 12 || total == 1 || total == 2) { document.getelementbyid("banner-container").style.backgroundimage = "url('images/winter.png')"; } // spring else if (total >= 2 && total <= 6) { document.getelementbyid("banner-container").style.backgroundimage = "url('images/spring.png')"; } else { document.getelementbyid("banner-container").style.backgroundimage = "url('images/summer.png')"; } </script> </body> </html>
Comments
Post a Comment