JesseToxik Posted April 24, 2013 Share Posted April 24, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277235-uncaught-typeerror-cannot-set-property-innerhtml-of-null/ Share on other sites More sharing options...
Solution nogray Posted April 24, 2013 Solution Share Posted April 24, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/277235-uncaught-typeerror-cannot-set-property-innerhtml-of-null/#findComment-1426223 Share on other sites More sharing options...
JesseToxik Posted April 24, 2013 Author Share Posted April 24, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/277235-uncaught-typeerror-cannot-set-property-innerhtml-of-null/#findComment-1426224 Share on other sites More sharing options...
nogray Posted April 24, 2013 Share Posted April 24, 2013 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' Quote Link to comment https://forums.phpfreaks.com/topic/277235-uncaught-typeerror-cannot-set-property-innerhtml-of-null/#findComment-1426370 Share on other sites More sharing options...
JesseToxik Posted April 24, 2013 Author Share Posted April 24, 2013 Ok makes sense Thanks Quote Link to comment https://forums.phpfreaks.com/topic/277235-uncaught-typeerror-cannot-set-property-innerhtml-of-null/#findComment-1426435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.