ryohei ryohei - 10 months ago 32
CSS Question

How to show a message by an if-statement in Javascript

I have an

and inside this file, I wrote code in order to show a message which is "hello" here. My goal is to implement code that shows three types of message: "Good morning", "Good evening", and "Good night".

Follows my code:

<p id="show_time" class="time"></p>
window.onload = show_time('show_time');

function show_time(id) {
h = date.getHours();

if h == 22 {
document.getElementById(id).innerHTML = "hello";
setTimeout('show_time("' + id + '");','1000');
return true;

In this code I only wrote one case. The class, time, is a
property that contains font-size and font-family. The
gets present hour and if
is 22 , "hello" is going to be output. But this does not work.

It is so helpful if you could provide code that displays the three types of message.

Thank you

Answer Source

window.onload = function() {
  var h = new Date().getHours(),
      msg = 'morning';
  if (h > 12) { msg = 'afternoon'; } 
  if (h > 20) { msg = 'night'; }

  document.getElementById('show_time').innerHTML = msg;
<p id="show_time" class="time"></p>