getElementById in global scope gives just null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Börni

    #1

    getElementById in global scope gives just null

    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.
  • Michael Winter

    #2
    Re: getElementById in global scope gives just null

    On Wed, 05 Jan 2005 16:52:14 +0000, Börni <b.reiter@onlin ehome.de> wrote:
    [color=blue]
    > I'm just wondering why one cant do something like this outside of an
    > function:
    > alert(document. getElementById( "edit"));[/color]

    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.

    Comment

    • Martin Honnen

      #3
      Re: getElementById in global scope gives just null



      Börni wrote:

      [color=blue]
      > 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.[/color]

      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/

      Comment

      Working...