Solved

cannot get data using getElementById

Posted on 2006-07-03
4
186 Views
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;
        else
          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?

0
Comment
Question by:bogorman
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:bpmurray
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]);
0
 

Author Comment

by:bogorman
ID: 17031164
Hi,
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')
values[i]=elem.??????

Regards
Bian
0
 
LVL 15

Accepted Solution

by:
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;
   }
0
 

Author Comment

by:bogorman
ID: 17031570
Hi,
Thanks very much for all your help. Will assign the points
Regards
Brian
0

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 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