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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

623 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