Results 1 to 6 of 6

Thread: [RESOLVED] document.getElemendById() always returns null

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved [RESOLVED] document.getElemendById() always returns null

    Why does the following piece of code *always* return null?
    I cannot figure this out...

    Code:
    <body>
        <div>        
            <script language=javascript>            
                alert(document.getElementById("p"));            
            </script>
            <div id="p" style="WIDTH: 600px; HEIGHT: 450px">&nbsp;</div>
        </div>
    </body>


    - ØØ -

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: document.getElemendById() always returns null

    The getElementById function will only return the id's of elements already rendered. Put it underneath and it will work:
    HTML Code:
    <body>
        <div>        
            <div id="p" style="WIDTH: 600px; HEIGHT: 450px">&nbsp;</div>
            <script type="text/javascript">            
                alert(document.getElementById("p"));            
            </script>
        </div>
    </body>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: document.getElemendById() always returns null

    Thanks a lot. Not me needing it though. But I rated you anyway...

    - ØØ -

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: document.getElemendById() always returns null

    Quote Originally Posted by visualAd
    The getElementById function will only return the id's of elements already rendered. Put it underneath and it will work:
    HTML Code:
    <body>
        <div>        
            <div id="p" style="WIDTH: 600px; HEIGHT: 450px">&nbsp;</div>
            <script type="text/javascript">            
                alert(document.getElementById("p"));            
            </script>
        </div>
    </body>

    ok..

    Adam, what if the code of getElementyById is in the head section of the code?

    See, I have an instance where I focus the next element (which is rendered above/before the current element) by using getElementById to access it, the problem it seems is that it cannot find that element.

    Does getElementById work for those elements which are rendered next/below the current element?

    Somehow, that seems to be the case in IE.


    What's the difference with accessing elements via Name and accessing elements via ID?

  5. #5
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: document.getElemendById() always returns null

    I'm just gonna make a new thread for this.

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: document.getElemendById() always returns null

    nevermind, it was a bug in my code afterall. the function getElementById works fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width