This is my first post so forgive me if it's duplicated.
I am trying to change the background image of an HTML document based on the time of the day. So far, I achieved to do so with document.write however, the output is not an image I can make responsive to the layout (cover/contain or width:100vh height 100vh) with CSS.
The code I have working is the following:
<html>
<head>
<style>
</style>
</head>
<script>
var myDate = new Date();
var hrs = myDate.getHours();
var backgroundimage;
if (hrs < 12)
document.write("<img src='/images/TomasGR/bg-by-time/background-daytime/images/morning.jpg'>");
else if (hrs >= 12 && hrs <= 17)
document.write("<img src='/images/TomasGR/bg-by-time/background-daytime/images/afternoon.jpg'");
else if (hrs >= 17 && hrs <= 24)
document.write("<img src='/images/TomasGR/bg-by-time/background-daytime/images/evening.jpg'>");
backgroundimage
</script>
</html>
So I tried modifying the code so I can use the label as a URL but at the end the result is only the display of the path to the file depending on the time of the day.
<html>
<head>
</head>
<style>
</style>
<body>
<div id="lblbackgroundimage"></div>
</body>
<script>
var myDate = new Date();
var hrs = myDate.getHours();
var backgroundimage;
if (hrs < 12)
backgroundimage = '/images/TomasGR/bg-by-time/background-daytime/images/morning.jpg';
else if (hrs >= 12 && hrs <= 17)
backgroundimage = '/images/TomasGR/bg-by-time/background-daytime/images/afternoon.jpg)';
else if (hrs >= 17 && hrs <= 24)
backgroundimage = '/images/TomasGR/bg-by-time/background-daytime/images/evening.jpg';
document.getElementById('lblbackgroundimage').innerHTML =
backgroundimage;
</script>
</html>
I've been reading many similar posts but I cannot find how to do it or what I'm doing wrong.
Any help would be greatly appreciated.
PS. I am doing it within a CMS inserting it as an HTML module.