Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 8, 2009, 05:45   #1
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
getElementById returns null

Hi,

Suppose I have the following inside a function:

JavaScript Code:
document.getElementById('display_content').innerHTML = "hello";

The above works. However, when I define it like the following it says display_content is null on firefox error console.

JavaScript Code:
var display_content = document.getElementById('display_content');
 
function render()
{
    display_content.innerHTML = "hello";
}

Any suggestions? when I replace display_content with document.getElementById('display_content'), it works.
cssExp is offline   Reply With Quote
Old Nov 8, 2009, 06:28   #2
markbrown4
padawan
 
markbrown4's Avatar
 
Join Date: Jul 2006
Location: Victoria, Australia
Posts: 3,029
Hi, make sure the html has finished loading before you run the script.
Code:
window.onload = function() {
  var display_content = document.getElementById('display_content');
}
function render() {
    display_content.innerHTML = "hello";
}
markbrown4 is online now   Reply With Quote
Old Nov 8, 2009, 08:21   #3
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
It still gives the same error, but if I define the function within the onload then it works

Code:
 window.onload = function()
{

  var display_content = document.getElementById('display_content');

  function render()
  {
      display_content.innerHTML = "hello";
  }
}
Is it possible without me having to define the function within onload?

Quote:
Originally Posted by markbrown4 View Post
Hi, make sure the html has finished loading before you run the script.
Code:
window.onload = function() {
  var display_content = document.getElementById('display_content');
}
function render() {
    display_content.innerHTML = "hello";
}
cssExp is offline   Reply With Quote
Old Nov 8, 2009, 11:17   #4
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 10,814
The display_content in the window.onload anonymous is defined local to that function. It no longer exists by the time the render function tries to access a global variable by the same name.
felgall is offline   Reply With Quote
Old Nov 8, 2009, 13:37   #5
markbrown4
padawan
 
markbrown4's Avatar
 
Join Date: Jul 2006
Location: Victoria, Australia
Posts: 3,029
You should try to avoid using global variables too so the following is probably the best.
If functions need references to other objects just pass them as parameters.
javascript Code:
function render() {
  var content = document.getElementById('display_content');
  display_content.innerHTML = "hello";
}
markbrown4 is online now   Reply With Quote
Old Nov 8, 2009, 20:30   #6
cssExp
SitePoint Addict
 
Join Date: Jun 2007
Posts: 266
defining the objects locally does the trick, thanks.
cssExp is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 21:05.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved