Reputation: 173
HTML:
<html>
<body>
<button onclick="bgTime()">CHANGE BACKGROUND</button>
</body>
<script type="text/javascript" src="script.js">
</script>
</html>
JavaScript:
function bgTime() {
var d = new Date();
if (d getHours() >= 21) {
document.body.style.backgroundImage = "url('bg/H.png')"
}
else if (d getHours() >= 18) {
document.body.style.backgroundImage = "url('bg/G.png')"
}
else if (d getHours() >= 15) {
document.body.style.backgroundImage = "url('bg/F.png')"
}
else if (d getHours() >= 12) {
document.body.style.backgroundImage = "url('bg/E.png')"
}
else if (d getHours() >= 9) {
document.body.style.backgroundImage = "url('bg/D.png')"
}
else if (d getHours() >= 6) {
document.body.style.backgroundImage = "url('bg/C.png')"
}
else if (d getHours() >= 3) {
document.body.style.backgroundImage = "url('bg/B.png')"
}
else {
document.body.style.backgroundImage = "url('bg/A.png')"
};
};
Okay so I am probably just being an idiot somehow, but I cannot find what I am doing wrong. I get two errors:
P.S. This is just a project for learning JS, don't judge lol.
Upvotes: 0
Views: 615
Reputation: 9157
You need to use dot notation on all of your if
statements:
if (d.getHours() >= 21) { ... }
bgTime
is not defined because the JS interpreter can't decode your program. Once you fix your if
s, everything should work fine.
References:
Upvotes: 1
Reputation:
Write
d.getHours()
getHours
is a method of d
which is accessed using the dot .
.
Upvotes: 2
Reputation: 700
You forgot a period. It should be:
d.getHours() >= 21
et cetera. :)
Upvotes: 1