We help IT Professionals succeed at work.

difference between document.surveym.chkIE.checked and document.getElementsByName('chkIE').checked

Medium Priority
496 Views
Last Modified: 2012-05-06
I try to use approved DOM2 methods so my code is cross-browser consistent (as much as can be expected).  I have a form with a checkbox control for a browser (say IE), followed by a selection control with several version numbers as the options with the selection.  I want to find out if the checkbox has been checked, and if so, look at the version number.

I don't understand why the .checked property doesn't work the same for two different ways to interrogate this, but I find that

document.getElementsByName('chkIE').checked evaluates to undefined
document.surveym.chkIE.checked evaluates to false

I can't get the results I want with getElementsByName inside nested if..else statements, but the form's name (surveym) works with no problem.  Can you explain the difference?



<script type="text/javascript">
. . .
var ieChecked = document.getElementsByName('chkIE').checked;
. . .
if (!ieChecked)
{
  alert("ieChecked is undefined");
}
else
{
  strValues += "\n" + "ieVersion = " + ieVersion;
  if (!ieVersion)
  {
    alert("no ieVersion");
    strMissingInfo += "\n     -  Explorer Version";
  }
  else
  {
    alert("ieVersion = " + ieVersion);
  }  // end if ieVersion
}  // end if ieChecked
. . .
</script>
. . .
. . .
      <label><input type="checkbox" name="chkIE" value="IE"/>Internet Explorer.....</label>
      <datalist name="IE_version">
        <select name="lstIE" size="1">
          <option value="99">-- select from list --</option>
          <option value="60">6.0</option>
          <option value="70">7.0</option>
          <option value="80">8.0</option>
          <option value="legacy">previous</option>
        </select><br/>
      </datalist>

Open in new window

Comment
Watch Question

getElementsByName returns an array(collection) of objects with the name. You need to use getElementsByName("chkIE")[0].checked if you want to get the checked property of the 1st element found.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
The best way to do is to use getElementById and define an id for your checkbox
<script type="text/javascript">
. . .
var ieChecked = document.getElementById('chkIE').checked;
. . .
if (!ieChecked)
{
  alert("ieChecked is undefined");
}
else
{
  strValues += "\n" + "ieVersion = " + ieVersion;
  if (!ieVersion)
  {
    alert("no ieVersion");
    strMissingInfo += "\n     -  Explorer Version";
  }
  else
  {
    alert("ieVersion = " + ieVersion);
  }  // end if ieVersion
}  // end if ieChecked
. . .
</script>
. . .
. . .
      <label><input type="checkbox" name="chkIE" id="chkIE" value="IE"/>Internet Explorer.....</label>
      <datalist name="IE_version">
        <select name="lstIE" size="1">
          <option value="99">-- select from list --</option>
          <option value="60">6.0</option>
          <option value="70">7.0</option>
          <option value="80">8.0</option>
          <option value="legacy">previous</option>
        </select><br/>
      </datalist>

Open in new window

Check this link if you need to know more about getElementsByName and getElementById
http://www.w3schools.com/htmldom/met_doc_getelementsbyname.asp
http://www.w3schools.com/HTMLDOM/met_doc_getelementbyid.asp
Alan VargaAccess Developer

Author

Commented:
sunithnair,

Thanks for your quick response.  This looks promising; I'll try working with the 0th array element and get back to you.

Since my form submissions are through Yahoo, I'm stuck using name attributes instead of id attributes.  When I substituted id for name the submission resulted in error messages from Yahoo.

Alan

Alan VargaAccess Developer

Author

Commented:
Understanding that I am working with a single-element array instead of a regular variable solved the problem.  My form is working much better.  Thanks for your help!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.