0

my instructions are to Include a function defined via a function expression that displays the current date and time in an h2 at the top of the page when the page is opened. Add an id=”dateDisplay” to the h2. Include a self-invoking function that changes the color of the date text that was printed by the first function.

Im a bit confused on how to accomplish this. I tried to do a function expression to display the date but i get nothing shown and no errors. I tried to do a body onload to call the function but that result in a console error of function not defined.

HTML

<div>
    <h2 id="dateDisplay"></h2>
</div>

Javascript

var d = new Date();
var x = function dateDisplay(){
    document.getElementById("dateDisplay").value = d;
}

I'm really confused by the instructions. What exactly am I suppose to do to get this in working order.

edited: I had an error with my link to my js file. The issue now is I get console error document.getElementById... is null weather i use innerHTML or value or .innerText

0

4 Answers 4

2

You need to call your function, and use .innerText and not .value. Note value is only for input elements.

var d = new Date();
var x = function dateDisplay(){
    document.getElementById("dateDisplay").innerText = d;
}

x();
Sign up to request clarification or add additional context in comments.

4 Comments

why does this work in jsfiddle, but not when i upload to my server? or do a preview view in komodo IDE?
@kronis72 Make sure you are running the JavaScript after the dateDisplay element is loaded.
I had an issue with my link to my js file, but your code example i get a console error that states document.getelementbyid is null
@kronis72 Then it's because your JavaScript is being ran before the element is being loaded. You can prevent that by placing the code inside window.onload = function(){ ... }. JSFiddle does that automatically by default. For example.
1

You can also use the .innerHTML method like so:

var d = new Date();
var x = function dateDisplay(){
    document.getElementById("dateDisplay").innerHTML = d;
}
x();

You also forgot to call the function x

Comments

1

you must use this code :
Javascript:

var d = new Date();
var x = function dateDisplay(){
document.getElementById("dateDisplay").innerHTML = d;
}
x();

To "display data" in HTML, (in most cases) you will set the value of an innerHTML property.after this change you must call x() function .
and you hava to use this after dateDisplay element loaded.

1 Comment

while this works in jsfiddle this does not work when on my server.
0

call function directly more prefer than assign to variable function

var d = new Date();
dateDisplay();
function dateDisplay(){
   document.getElementById("dateDisplay").innerHTML = d;
}

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.