Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

cannot get data using getElementById

Posted on 2006-07-03
4
Medium Priority
?
192 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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…
Progress

824 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