cannot get data using getElementById

Posted on 2006-07-03
Last Modified: 2010-04-17
I have a webpage with two forms on it. I have the following function on the page:

 var ids = new Array('schoolname', 'addr1', 'addr2', 'town', 'schoolarea', 'postcode','fax','faith','gender','schoolsize','schooltype','ages','county','schoolmap','route','stats');
 var values = new Array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
 var needToConfirm=true;
  function populateArrays()
       // assign the default values to the items in the values array
    for (var i = 0; i < ids.length; i++)
      var elem = document.getElementById(ids[i]);
        alert("elem = " + elem.value);
        alert("ids = " + ids[i]);
      if (elem)
        if (elem.type == 'checkbox' || elem.type == 'radio')
          values[i] = elem.checked;
          values[i] = elem.value;

The alert statements show the id names in the ids[] array but the variable elem seems to null or empty. Trying to display elem or elem.value just gives a blank
I have, of course, assigned the ids of the second form to the names in the ids[] array.
Where have I gone wrong?

Question by:bogorman
  • 2
  • 2
LVL 15

Expert Comment

ID: 17030815
Actually, you should use a "safe" way to get the element - getElementById isn't enough on its own:

elem = (window.document.getElementById)? window.document.getElementById(ids[i]) : window.document.all(ids[i]);

Author Comment

ID: 17031164
Yes, that seems to work fine.
The only thing is that some of the controls are listboxes.

a) how do I use getElementById to read the "bound column" value of the selected item?

b) and how would you write:
if (elem.type = 'listbox')

LVL 15

Accepted Solution

bpmurray earned 500 total points
ID: 17031503
Something like this?

   values[i] = "";
   if (elem.type == "select-multiple") {
      cnt = elem.options.length;
      for (var iX=0; iX<cnt; iX++) {
         if (elem.options[iX].selected) {
          values[i] += xxx.options[iX].value + ",";
   } else if (elem.type == "select-one") {
      values[i] = elem.value;

Author Comment

ID: 17031570
Thanks very much for all your help. Will assign the points

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 48
base64 decode encode 12 98
Program to display an alert on Windows Toolbar 2 65
Birthdays 3 14
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now