473,486 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

document.getElementById isn't working

risk32
98 New Member
Hi all. I'm not exactly sure what I'm doing wrong, but when I try to use the document.getElementById() method, it's not working. Here's what I have:

Expand|Select|Wrap|Line Numbers
  1. var username = document.getElementById('username')
  2.  
  3. function test()
  4.     {
  5.         document.write(username)
  6.     }
  7.  
  8. ...
  9.  
  10. Username: <input type="text" id="username">
  11. <input type="button" value="test" onClick="test();">
  12.  
(I am aware that I don't use braces in standard format, it just helps me keep track of what I'm doing)

When I click the button, the only thing it returns is null. Am I doing something wrong?
May 27 '10 #1
3 1455
Dormilich
8,658 Recognized Expert Moderator Expert
yes and no. if you call getElementById() before the document tree is finished (resp. the element in questions is attached to the DOM) you get null as result.
May 27 '10 #2
risk32
98 New Member
@Dormilich
Where would be the appropiate location for the document.getElementById() method?
May 28 '10 #3
risk32
98 New Member
I actually feel like an idiot. Even though the variable was assigned globally, it didn't read inside the function. So, I put it inside the function and added .value to the end for the alert. It's working. Now to figure out arrays...
May 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
20910
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...
1
2684
by: lawrence | last post by:
This PHP function prints out a bunch of Javascript (as you can see). This is all part of the open source weblog software of PDS (www.publicdomainsoftware.org). We had this javascript stuff...
7
5960
by: PaulB | last post by:
Good Morning everybody, I'm trying to adapt a tutorial script that will handle the behaviour of an "Expanding/Contracting" site-navigation menu. The code that seems to handle the expansion and...
2
4422
by: Earl Teigrob | last post by:
I am trying to build a custom control to wrap my smart navigation implimention (not microsofts 'cause it has problems) The follow code works fine when the onclick and onload events are defined in...
2
1267
by: PrinceMhul | last post by:
var objListBox = document.getElementById('catalist'); var val = objListBox.value; var txt = dyevalue00.value; document.getElementById(val).value = txt; To explain real quick, this is for a...
2
1830
by: yogarajan | last post by:
hi i have create one aspx file with masterpage. i have using javascript code so in javascript code contains document.gelelementbyid but it gives error (document.getelementbyid is null or...
29
19135
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
10
3723
by: AC | last post by:
I had a page that does some event setup on window.onload: function prepEvents() { document.getElementById("menumap_sales").onmouseover = swapMenuSales; // etc } window.onload = prepEvents;
8
15330
by: Linto81 | last post by:
Please help... this is doing my head in! I have some gifs within a page that when clicked on switch to an animated version. The script below works great in FF or safari... but not in ie. Any tips...
3
2910
by: Muddasir | last post by:
Hello guyz Can you please help me with this. I am having problems with document.getElementById. It is working fine in FireFox but in IE its not working. What should i do. Thanks
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.