473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getElementbyID is null

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>

Jun 5 '07 #1
3 2313
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.
Jun 5 '07 #2
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
Jun 5 '07 #3
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 <zun...@inf.ufsc.br>
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

Jun 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Mayor | last post by:
Hi, I'm back again. Basically i'm trying to draw a box over an image which is turning out to be a nightmare. The problem i'm getting at the moment is that i'm creating a line with <div which works...
12
by: bcr07548 | last post by:
I am writing a web site that uses JavaScript to validate certain forms and I seem to be having some trouble. The site uses PHP and for one of the forms, depending on the situation, one of of the...
5
by: HopfZ | last post by:
I made two shortcut functions for document.getElementById as: function EBI2(id){return document.getElementById(id)}; var EBI3 = document.getElementById; But EBI3 don't work. EBI2('myText');...
3
by: RobertoColombo | last post by:
Hello all, I have some simple JavaScript code that changes an image based on some user action (i.e. pushing a button). I sniff the broiwsers and, in case of IE5+, I use GetElementById and then...
4
by: raknin | last post by:
Hi, I am creating dynamic list in the server through php and insert it using ajax. for ajax I using the following statement: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.