Jump to content

Uncaught TypeError: Cannot set property 'innerHTML' of null


JesseToxik
Go to solution Solved by nogray,

Recommended Posts

Hey guys I'm at a bit of a snag.

I have a script on this page [ http://vampire-news.webs.com/quote.htm ] which is supposed to show a different quote each day of the year. I have an array of 366(for leap years so the script does not fail) quotes. Each day it should display a different one. To write the quote I have a div with an ID of dayQuote and I am using document.getElementById("dayQuote").innerHTML = Quote[DOY]; to write the quote to the desired div. 

The issue is it doesn't work. 

In Chromes javascript console I get the following error "Uncaught TypeError: Cannot set property 'innerHTML' of null"

 

I don't know what I am doing wrong.

 

Thanks for any help. :)

 

 

 

Link to comment
Share on other sites

  • Solution

You are assigning the results of writeQuote() to your onload event rather than assigning the function itself. Change

 

window.onload = writeQuote();

 

to

 

window.onload = writeQuote;

 

 

 

Link to comment
Share on other sites

You are assigning the results of writeQuote() to your onload event rather than assigning the function itself. Change

 

window.onload = writeQuote();

 

to

 

window.onload = writeQuote;

 

 

 

Thanks that fixed it :)

I never understood what removing or adding the parenthesis changed.

Would you mind explaining the difference?

Link to comment
Share on other sites

When you include parenthesis you are telling the browser to execute the function and assign the return value to the variable. And when you omit the parenthesis, you are telling the browser to assign the function itself to a variable. For example

function returnA(){
    return 'A';
}

var my_A = returnA(); // my_A will be a string 'A';

var my_A_func = returnA; // my_A_func will be a function that returns 'A'

// using the new variables
alert(my_A); // alerts 'A';
alert(my_A_func) // alerts the function code
alert( my_A_func() ) // alerts 'A'
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.