2

The code should be fairly simple but im not sure why its not working its making me go insane. It should just get the clock id and then just change its background dynamically based on the time. The code i have is

init();
function init(){
var clock = document.getElementById('clock');
var currentdate = new Date();
var datetime = currentdate.getHours();
if(datetime==1||datetime==13){
clock.style.backgroundImage="url(clock/clock1.png)";
}
else if(datetime==2||datetime==14){
clock.style.backgroundImage="url(clock/clock2.png)";
}
else if(datetime==3||datetime==15){
clock.style.backgroundImage="url(clock/clock3.png)";
}
else if(datetime==4||datetime==16){
clock.style.backgroundImage="url(clock/clock4.png)";
}
else if(datetime==5||datetime==17){
clock.style.backgroundImage="url(clock/clock5.png)";
}
else if(datetime==6||datetime==18){
clock.style.backgroundImage="url(clock/clock6.png)";
}
else if(datetime==7||datetime==19){
clock.style.backgroundImage="url(clock/clock7.png)";
}
else if(datetime==8||datetime==20){
clock.style.backgroundImage="url(clock/clock8.png)";
}
else if(datetime==9||datetime==21){
clock.style.backgroundImage="url(clock/clock9.png)";
}
else if(datetime==10||datetime==22){
clock.style.backgroundImage="url(clock/clock10.png)";
}
else if(datetime==11||datetime==23){
clock.style.backgroundImage="url(clock/clock11.png)";
}
else if(datetime==0||datetime==12){
clock.style.backgroundImage="url(clock/clock12.png)";
}
}

and the HTML is

<div id="clock"><img src="clock/pill.png" alt="pill image" id="pillpic"/></div>
2
  • What's happening that is not correct? What are the symptoms? Use the Dev Console or something to check the background of the div. Is the background image value changing? Is the image not found? Commented Dec 12, 2013 at 22:50
  • Not too familiar with setting css using javascript but you could try setting the css with jQuery instead. $(clock).css('background-image','url(clock/clock1.png)') Are all your if statements getting executed? Commented Dec 12, 2013 at 22:51

2 Answers 2

2

Your code will work fine as long as it's running after the clock element has loaded. Be sure to put your script below that element, or use an event handler that runs after the DOM is loaded.

Here's a cleaned up version of your code.

init();

function init() {
    var clock = document.getElementById('clock');
    var t = (new Date().getHours() % 12) || 12;

    clock.style.backgroundImage = "url(clock/clock" + t + ".png)";
}

DEMO: http://jsfiddle.net/EnN6s/

Also make sure your image path is correct.

Sign up to request clarification or add additional context in comments.

2 Comments

perfect, it works now with your code and probably mine too, yours is just a lot neater! I was just calling init(); in the wrong place. cheers
@user2232418: Ah, yeah that'll do it! Glad you got it working.
0
function init(){
var clock = document.getElementById('clock');
var currentdate = new Date();
var datetime = currentdate.getHours();
    datetime = datetime >= 12 ? datetime-12 : datetime;
    clock.style.backgroundImage="url(clock/clock"+datetime+".png)";
}
init();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.