getElementById in global scope gives just null

M

Martin Honnen

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>
 
M

Michael Winter

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
 
B

Börni

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.
 
B

Börni

Michael said:
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
Thats quiet obvious.
I think i need some sleep ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top