getElementbyID is null

D

Deere

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

-Lost

Deere said:
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.
 
N

Ney André de Mello Zunino

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,
 
D

Deere

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

thanks for the help.
 

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,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top