-6

I want my site to display "Open" or "Closed" based on the current time.

My question is nothing is showing up on the site, so i'm trying to figure out whats wrong with the code

<script type="text/javascript">
var today = new Date(),
var open = "Open",
var closed = "Closed",
var display = document.getElementById('hours-display'); 
if (today.getHours() >= 9 && today.getHours() < 18) {
display.innerHTML = open;
} else {
display.innerHTML = closed;
}
</script>


<div id="hours-display"></div>
Kevin
  • 31
  • 2
  • 5

2 Answers2

0

You can only getElementById once the specified element has been declared. Additionally you used commas instead of semi-colons

<div id="hours-display"></div>
<script>
  var today = new Date();
  var display = document.getElementById("hours-display");
  if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = "Open";
  } else {
    display.innerHTML = "Closed";
  }
</script>
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • Care to explain the changes or why this might be useful? Or do you expect people to just use it and not care to understand the changes? – NewToJS Jul 18 '17 at 18:56
  • It's for a client, Car dealership, they really wanted it – Kevin Jul 18 '17 at 18:58
0

The way you declare your variables are the issue. You should remove the var from the other variables since you are using commas like this:

var today = new Date(),
open = "Open",
closed = "Closed",
display = document.getElementById('hours-display'); 
if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = open;
} else {
    display.innerHTML = closed;
}
Richboy
  • 33
  • 4