By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

getElementById in global scope gives just null

P: n/a
Hi,
I'm just wondering why one cant do something like this outside of an
function:
alert(document.getElementById("edit"));

this just gives null, but inside a function it works.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 05 Jan 2005 16:52:14 +0000, Börni <b.******@onlinehome.de> wrote:
I'm just wondering why one cant do something like this outside of an
function:
alert(document.getElementById("edit"));


You can. More than likely the element, edit, hasn't been parsed by the
browser when the getElementById call is made. This will happen if, for
example, the relevant SCRIPT element is in the document head.

Create a SCRIPT element after the closing tag of 'edit' and include your
code.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a


Börni wrote:

I'm just wondering why one cant do something like this outside of an
function:
alert(document.getElementById("edit"));

this just gives null, but inside a function it works.


Well if it gives null it does work too, it just means no element with
that id can be found, presumably as the script is called somewhere
before the element is parsed e.g.
<script type="text/javascript">
alert(document.getElementById("edit"));
</script>
<div id="edit">...</div>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.