Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:34:06+00:00 2026-06-11T15:34:06+00:00

The innerHTML isn’t working in this JavaScript code. var date = new Date(); var

  • 0

The innerHTML isn’t working in this JavaScript code.

var date = new Date();
var time = date.getHours();
var morning = document.p.getElementById("greeting").innerHTML="Good Morning ";
var afternoon = document.write =  "Good Afternoon ";
var evening = document.write = "Good evening";

if (time < 12){
    morning}
else if (time < 18){
afternoon}
else {evening};

The HTML code is

<p id="greeting"> </p>

I am trying to get the code to read “good morning” or “afternoon”, based on the time. The JavaScript is in an external file. It worked with document.write but in the external file, the greeting appears at the top of the page.

Here I am trying to set a

of ‘greeting’ based on time of day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:34:07+00:00Added an answer on June 11, 2026 at 3:34 pm
    var afternoon = document.write = "Good afternoon";
    

    What this does is to introduce a variable called afternoon and to set both it and the property write on document, which was previously a function call, to the string "Good afternoon".

    At a later point in your code, you just write afternoon, which accesses the variable, but doesn’t do anything with it.

    It looks like you want to do something like this:

    var morning = function() { document.getElementById("greeting").innerHTML="Good Morning "; };
    var afternoon = function() { document.getElementById("greeting").innerHTML="Good Afternoon "; };
    var evening = function() { document.getElementById("greeting").innerHTML="Good evening"; };
    
    if (time < 12) {
       morning();
    } else if (time < 18) {
       afternoon();
    } else {
       evening();
    };
    

    That is, the above would be the closest thing to your code, that is somewhat correct. I would still say that there is room for some improvement. Something like the following would make more sense to me:

    var morning = 'Good Morning ';
    var afternoon = 'Good Afternoon ';
    var evening = 'Good evening';
    
    if(time < 12) {
        document.getElementById("greeting").innerHTML = morning;
    } else if (time < 18) {
        document.getElementById("greeting").innerHTML = afternoon;
    } else {
        document.getElementById("greeting").innerHTML = evening;
    }
    

    Better yet, you could probably decide which message you want to use first, and then assign it, which would give you fewer places to change your code if you want to change where to output the greeting, for instance:

    var messages = {
       morning: 'Good Morning ',
       afternoon: 'Good Afternoon ',
       evening: 'Good evening'
    };
    
    var greeting = time < 12 
        ? messages.morning 
        : time < 18 
            ? messages.afternoon 
            : messages.evening;
    
    document.getElementById('greeting').innerHTML = greeting;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: function text(var text) { var Value = document.getElementById(display).innerHTML; var New
var json_string=$.parseJSON(document.getElementById(json_db).innerHTML); $(function () { $(#folder_tree).jstree({ contextmenu: {items: customMenu}, json_data : {data:json_string}, plugins :
This isn't my code, I'm just troubleshooting it. Some code that has been working
I'm trying to recreate the following code with jQuery: var node = document.createElement(div); node.innerHTML
I have some code doing this : var changes = document.getElementsByName(from); for (var c=0;
I've been at this for hours but it just isn't working. I'm trying to
For some reason this isn't working? I want to fill the ul#list with some
My understanding of innerHTML isn't completely clear so I'm having some trouble. This produces
var test = document.body.getElementsByTagName(a); for (var i=0; i<test.length; i++) if(test[i].innerHTML().indexOf(search string) != -1){test[i].style.color=black;} Hopefully
Does anybody know why the following code isn't working in IE? (working nice in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.