|
|
document.getElementById always returns null
|
|
|
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jul 2003
Location: Seattle, WA
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Nov 1999
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
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...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|