Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cannot get data using getElementById

Posted on 2006-07-03
4
Medium Priority
?
190 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
[X]
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
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

715 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