Sheperd Sheperd - 6 months ago 12
HTML Question

Change div background on dytime basis

I am having trouble changing my jumbotron background at specific day times, but nothing is showing and the jumbotron does not have any pictures as background.

My Js:

var currentTime = new Date().getHours();
if (7 <= currentTime && currentTime < 20) {
if (document.getElementById("jumbotron")) {
document.getElementById("jumbotron").background = "1.jpg";
}
}else {
if (document.getElementById("jumbotron")) {
document.getElementById("jumbotron").background = "2.jpg";
}
}

Answer

You need to use correct syntax:

document.getElementById("jumbotron").style.backgroundImage = "url('1.jpg')";

http://www.w3schools.com/jsref/prop_style_backgroundimage.asp

Full sample is here:

https://jsfiddle.net/gevorgha/15baqpjt/