Sheperd Sheperd - 2 years ago 81
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 Source

You need to use correct syntax:

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

Full sample is here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download