469,295 Members | 1,959 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,295 developers. It's quick & easy.

document.getElementById isn't working

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')
  3. function test()
  4.     {
  5.         document.write(username)
  6.     }
  8. ...
  10. Username: <input type="text" id="username">
  11. <input type="button" value="test" onClick="test();">
(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 1276
8,651 Expert Mod 8TB
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
Where would be the appropiate location for the document.getElementById() method?
May 28 '10 #3
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

Post your reply

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

Similar topics

2 posts views Thread by Gary Mayor | last post: by
7 posts views Thread by PaulB | last post: by
2 posts views Thread by Earl Teigrob | last post: by
2 posts views Thread by PrinceMhul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.