0

I am suppose to use a function expression to display the date when the page loads then use a self invoking function to change the date font color.

I thought I was on the right path but I have two issues. The first one is, my code doesnt change the color of text. the second one, is i get a console error from the first function saying the dateDisplay(); is not defined. but if I try to remove the function name from the call and just use (); to run the function. I get the error

document.getElementById("dateDisplay").innerHTML = d; is null

I had tried to change several things and I just wind up with console errors. generally i either get function not defined, or an error like the one above.

HTML

<div>
 <h2><p id="dateDisplay"></p></h2>
 <p>does this work</p>
</div>

javascript

var d = new Date();
 window.onload = function dateDisplay(){
 document.getElementById("dateDisplay").innerHTML = d;
}
();
(function (){
 document.getElementById("dateDisplay").style.color="red";
})();

Im quite lost and dont understand whats going on.

asked Sep 17, 2015 at 0:44
3
  • I pasted you code like it is into codepen and it worked :) Commented Sep 17, 2015 at 1:01
  • You shouldn't have the (); on the 5th line of your JavaScript. Also, if your script block is above the div where the dateDisplay element is defined, then the element will not exist at the time that the self-invoking function expression is executed. Commented Sep 17, 2015 at 1:59
  • This question is similar to: Why does jQuery or a DOM method such as getElementById not find the element?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 26, 2024 at 13:15

2 Answers 2

2

Do this instead:

window.onload = function(){
 var d = new Date();
 document.getElementById("dateDisplay").innerHTML = d;
 document.getElementById("dateDisplay").style.color = "red";
};

or even better:

window.addEventListener("load", function(){
 var d = new Date();
 document.getElementById("dateDisplay").innerHTML = d;
 document.getElementById("dateDisplay").style.color = "red";
});

or you can also do:

document.addEventListener("DOMContentLoaded", function(){
 var d = new Date(),
 display = document.getElementById("dateDisplay");
 display.innerHTML = d;
 display.style.color = "red";
});
answered Sep 17, 2015 at 0:47
Sign up to request clarification or add additional context in comments.

4 Comments

Where is the self-invoking function?
There is no self invoking functions. You don't need them anyway since it's the browser's responsibility to execute your code at the right time.
But the homework assignment (I assume that's what this is) says to use a self-invoking function. You can't change the assignment just because it's not the way you would code it.
I don't actually see any places you would want to use a self invoking function...
0

Are you doing homework that requires you to do it a certain way?

Anyway I thought I'd give a couple pointers to help out. First off, if something doesn't work and I don't understand it much (still learning it) I like to break it down to smaller parts and test until I get a better understanding.

For instance try just one of your functions with console.log until you get it working:

var d = new Date();
window.onload = function() {
 console.log('window loaded!');
}

This function should work just fine when you load, if it does not then something else is wrong. Once you get this working then add in the document.getElem... and work on the next function etc.

edit, showing my quick test:

HTML:

<!DOCTYPE html>
<html>
 <head>
 </head>
 <body>
 <div id="header"></div>
 </body>
 <script src="test.js"></script>
</html>

javascript:

var d = new Date();
window.onload = function() {
 console.log('window loaded');
 document.getElementById('header').innerHTML = d;
}
(function() {
 console.log('invoked');
 document.getElementById('header').style.color = 'red';
}());

worked just fine.

answered Sep 17, 2015 at 1:05

8 Comments

yes this is a homework assignment, so I assume I have to do it a certain way, but the instructions are very confusing and vague
Your code up top, though not ideal, does work. Depending on your instructor, he/she might like if you found another route. I was always years ahead of my class so when he asked for a text game, I made a 2d game. However, since you're just learning you might want to at least figure out hwy your code didn't work for you first.
well could you help me out a bit, cause i dont understand why it doesnt work. i get console errors I stated in my question. I dont really like the way the instructions state, seems very sloppy
i get an error thrown saying dateDisplay element value is null, when Im trying to set the value
It's hard for me to say, is that 100% of your code?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.