getElementbyID is null

Discussion in 'Javascript' started by Deere, Jun 5, 2007.

  1. Deere

    Deere Guest

    I have been trying to put some data in "Text1" in trying to figure out
    my problem I'm showing the element comes back as null...why?


    <script type="text/javascript">
    elem = document.getElementById('Text1');
    alert(elem) //..shows null
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <input id="Text1" type="text" />
    </form>
    </body>
    </html>
    Deere, Jun 5, 2007
    #1
    1. Advertising

  2. Deere

    -Lost Guest

    Deere wrote:
    > I have been trying to put some data in "Text1" in trying to figure out
    > my problem I'm showing the element comes back as null...why?
    >
    >
    > <script type="text/javascript">
    > elem = document.getElementById('Text1');
    > alert(elem) //..shows null
    > </script>
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <input id="Text1" type="text" />
    > </form>
    > </body>
    > </html>


    1. Has the DOM finished loading?

    If no, then why expect it to return anything but null. null is the clue
    that it is non-existent.

    2. You should use existing collections rather than getElementById,
    getElementsByTagName, or any other variants.

    document.forms['form1'].elements['Text1']; // [object HTMLInputElement]

    3. I am not entirely sure what IIS/ASP'ish monstrosity you are going
    for, but you should always use clean and valid markup.

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
    -Lost, Jun 5, 2007
    #2
    1. Advertising

  3. Deere escreveu:

    > I have been trying to put some data in "Text1" in trying to figure out
    > my problem I'm showing the element comes back as null...why?


    Either move your script below the referred element or, better yet, place
    the code in the body element's 'onload' event handler.

    I hope that helps,

    --
    Ney André de Mello Zunino
    Ney André de Mello Zunino, Jun 5, 2007
    #3
  4. Deere

    Deere Guest

    Re: getElementbyID is null

    I tried putting script below the element and that worked. I'll try
    the other suggestions as well.

    thanks for the help.



    On Jun 5, 3:04 pm, Ney André de Mello Zunino <>
    wrote:
    > Deere escreveu:
    >
    > > I have been trying to put some data in "Text1" in trying to figure out
    > > my problem I'm showing the element comes back as null...why?

    >
    > Either move your script below the referred element or, better yet, place
    > the code in the body element's 'onload' event handler.
    >
    > I hope that helps,
    >
    > --
    > Ney André de Mello Zunino
    Deere, Jun 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,623
    Mike Schilling
    Mar 29, 2006
  2. =?Utf-8?B?QmlsbHk=?=

    document.getElementById("ReportViewerID") returns null

    =?Utf-8?B?QmlsbHk=?=, Jul 6, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    20,502
    Erik Funkenbusch
    Jul 7, 2006
  3. Rob
    Replies:
    4
    Views:
    629
    Patrice
    Jan 23, 2008
  4. Gary Mayor

    document.getElementById comes out NULL help!

    Gary Mayor, Mar 2, 2004, in forum: Javascript
    Replies:
    2
    Views:
    164
    Gary Mayor
    Mar 3, 2004
  5. Martin Honnen

    getElementById in global scope gives just null

    Martin Honnen, Jan 5, 2005, in forum: Javascript
    Replies:
    4
    Views:
    79
    Börni
    Jan 5, 2005
Loading...

Share This Page