Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > document.getElementById always returns null

document.getElementById always returns null
Thread Tools
Millennium
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Sep 20, 2004, 06:23 AM
 
I'm having a problem using document.getElementById in Firefox and Safari: it's always returning null in any page I write, whether or not the ID actually exists in the document. This has been happenning in pages that I know are validating, so clearly I'm doing something wrong, but I can't figure out what it is.

Here's some sample code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>getElementById Test</title> <script type="text/javascript"> <!-- alert(document.getElementById("hello")); --> </script> </head> <body> <a href="#" id="hello">Hello</a> </body> </html>
Anyone have any ideas? I can't find anything immediately wrong with this, but if Firefox and Safari are both failing then I know there must be something I'm missing.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
ntt
Fresh-Faced Recruit
Join Date: Jul 2003
Location: Seattle, WA
Status: Offline
Reply With Quote
Sep 20, 2004, 09:08 AM
 
The script should be _after_ the id is defined. At the point the script runs, the id doesn't exist. Try putting the script before the closing body tag.
     
Millennium  (op)
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Sep 20, 2004, 10:04 AM
 
That would make sense, but it seems to be a problem even after the page is fully loaded. I can type the following into Firefox's JavaScript Console even after the page has loaded...
Code:
document.getElementById("hello")
...and it still returns null.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Sep 20, 2004, 11:31 AM
 
seems to work just fine if you put the JS in a function and call it when the page is done loading:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>getElementById Test</title> <script type="text/javascript"> <!-- function doIt() { alert(document.getElementById("hello")); } --> </script> </head> <body onload="doIt();"> <a href="#" id="hello">Hello</a> </body> </html>
The only thing necessary for evil to flourish is for good men to do nothing
- Edmund Burke
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Sep 21, 2004, 01:17 PM
 
Even if you call afterwards, as ntt said:-

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>getElementById Test</title> </head> <body> <div id="hello">Hello</div> <script type="text/javascript"> <!-- alert(document.getElementById("hello")); --> </script> </body> </html>
Computer thez nohhh...
     
   
Thread Tools
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 04:48 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,