I need to change the class of an object based on the time, with two classes available: recommendedspot
and notrecommended
. The code I've written so far is not displaying the image correctly. Can someone help me troubleshoot this issue?
Here is my current code:
var time = new Date().getHours();
if (time > 9 && time < 17) {
document.getElementById("spotsouth").classList.remove('notrecommended');
document.getElementById("spotsouth").classList.add('recommendedspot');
} else if (time > 6 && time < 9) {
document.getElementById("spotnorth").classList.remove('notrecommended');
document.getElementById("spotnorth").classList.add('recommendedspot');
} else if (time > 17 && time < 21) {
document.getElementById("spotnorth").classList.remove('notrecommended');
document.getElementById("spotnorth").classList.add('recommendedspot');
} else {}
.notrecommended {
display: none;
}
.recommendedspot {
display: inline;
margin-left: 15px;
max-width: 50px;
}
<img id="spotsouth" src="site-logo.png" alt="spot south" class="notrecommended">
<img id="spotnorth" src="site-logo.png" alt="spot north" class="notrecommended">
If you spot where I went wrong, please share your insights as I am struggling to resolve this issue. Thank you in advance.