0

I am trying to add text to my site based on the time. However, I cannot get it to work!

HTML:

<!doctype>
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css">
<script link="index.js"></script>
</head>
<body>

<div id="header">
    <img id="bannerImage" src="banner.jpg" align="middle"></img>
</div id="header">
<section id="master">

<div id="navigation">
    <ul>
        <li class="navObject">About Me</li>
        <li class="navObject">Products</li>
        <li class="navObject">Contact</li>  
        <li class="navObject">FAQ</li>
    </ul>
</div>
<div>
<h3 id="welcomeText"></h3>
</div>
</section>
</body>
</html>

JS:

 var timeAdd = function(){
                        var today = new Date();
                        var todayHour = today.getHours();
                        var message;

                        switch(todayHour){
                            case(todayHour > 18):
                                message = "Good Evening!";
                                break;
                            case(todayHour > 12):
                                message = "Good Afternoon!";
                                break;
                            case(todayHour > 0):
                                message = "Good Morning!";
                                break;
                            default:
                                message = "Welcome!";
                                break;
                            }
                            return message;
                            };
var tag = document.getElementById("welcomeText");
tag.textContent(timeAdd());

I just want to be able to add the message to said div. When I try the page, nothing comes up. I have tried different ways, so, what am I missing?

9
  • 2
    tag.innerHTML = timeAdd() should work. Commented Dec 11, 2014 at 16:55
  • 1
    You want to use switch(true) Commented Dec 11, 2014 at 16:57
  • @Antiga See stackoverflow.com/a/3463944/338803 Commented Dec 11, 2014 at 17:00
  • 1
    @ColbyCox Before you post a question about JavaScript, try using Developer Tools console in your browser. In this case, it would have told you that the textContent property is not a function. Commented Dec 11, 2014 at 17:00
  • 1
    @ColbyCox You will also need to move your <script link="index.js"></script> tag before </body> since your JavaScript will be executed before the DOM is loaded. Also, <script src="index.js"></script> instead of <script link="..."></script> Commented Dec 11, 2014 at 17:05

4 Answers 4

2

Your assignment is wrong - it should be done in the following way:

tag.textContent = timeAdd();

better support however has innerHTML. You can use it the same way like textContent.

tag.innerHTML = timeAdd();

Also, you switch does not work. You have to use switch(true), in order for it to work properly. Personally, I'd rather go with an if else which looks cleaner to me:

if (todayHour > 18){
   message = "Good Evening!";
} else if (todayHour > 12){
   message = "Good Afternoon!";
} else if (todayHour > 0){
   message = "Good Morning!";
} else {
   message = "Welcome!";
}

EDIT: Also, you have to care for the order of your script execution. Generally, you can include your index.js in the head, however this part:

var tag = document.getElementById("welcomeText");
tag.textContent =timeAdd();

has to be executed after you introduce <h3 id="welcomeText"></h3> in your markup. So either you call index.js after that element, or execute just these two lines afterwards:

<h3 id="welcomeText"></h3>
<script src="index.js"></script>

or

<script src="index.js"></script> <!-- without the last two lines! -->
<h3 id="welcomeText"></h3>
<script>
    var tag = document.getElementById("welcomeText");
    tag.textContent =timeAdd();
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

@ColbyCox If all your problems are settled now you can mark your questions as solved to the SO community by accepting an answer as solution be ticking the checkmark right below the score.
0

textContent is a property, not a method.
Also if you use boolean cases to match first you should use true as switch argument

Try this:

var timeAdd = function(){
  var today = new Date();
  var todayHour = today.getHours();
  var message;
  switch(true){
    case(todayHour > 18):
      message = "Good Evening!";
      break;
    case(todayHour > 12):
      message = "Good Afternoon!";
      break;
    case(todayHour > 0):
      message = "Good Morning!";
      break;
    default:
      message = "Welcome!";
      break;
  }
  return message;
};

var tag = document.getElementById("welcomeText");
tag.textContent = timeAdd();

Comments

0

Instead set the innerHtml property, text or html

tag.innerHTML = timeAdd()

2 Comments

where do you have that info from? Sure an h3 has the textContent property.
So you should remove your answer then?
0

There are a few issues here. First, remove the id="header" from the closing div tag; that is invalid HTML.

Second, the switch statement is not working the way you think it is. Use this as your timeAdd function instead (that name doesn't make sense either, maybe getTimeGreeting instead:

var timeAdd = function() {
    var today = new Date();
    var todayHour = today.getHours();
    var message = 'Welcome!';

    if (todayHour >= 18) {
        message = 'Good Evening!';
    } else if (todayHour >= 12) {
        message = 'Good Afternoon!';
    } else if (todayHour >= 0) {
        message = 'Good Morning!';
    }

    return message;
};

Finally, change the setting line to tag.innerText = timeAdd();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.