473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.getEle mentById 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.getEle mentById() 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 1462
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.getEle mentById() 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
20927
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 when it's not hidden but I need to be able to make it hidden so I can use layers to show all when it finished drawing to make it smoother. This is how some other scripts are doing it that i've seen. So i've got it drawing a line and I need...
1
2694
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 working, but it only worked for IE. You can see a working version here: http://www.publicpen.com/designer/mcControlPanel.php username: designer password: designer123 However, I've tried to rewrite this so it would work in all browsers,
7
5977
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 contraction follows at the bottom of my message here. This following line is a big part of my hang-up: document.getElementById('m1').style.display='none';
2
4431
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 the Body tag. However, when I try to set them in Javascript code, I get errors. What am I doing wrong??? **********THIS WORKS ************ <html>
2
1272
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 character maker. The "catalist" is a listbox with different values such as "Body1" - "Head" - "Shoes" - "Hair" etc "dyevalue00" is a textbox I have many textboxes that are named the same as the "catalist" values
2
1845
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 not an object) pls anyone give me the solutions
29
19219
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 site and so far doing this has saved about 8KB of javascript (lots of ajax/dynamic elements). I just
10
3744
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
15332
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 here would save me hitting the bottle! the img tag is: <img src="/graphics/prmoff.gif" width="250" height="188" id="prm" onClick="animateMe('prm');" alt="Click to start/stop animation"> the code is: function animateMe(id){
3
2916
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
8145
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8556
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8410
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5526
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4037
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1407
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.