Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Length of HTML form object

    Hi,

    I found one thing.
    Assume that I have checkedbox object in my form:

    1.) If I have more than one checkbox object with the same name (i.e mycheckbox") I have no problem to get the length of the checkbox in javascript.

    2.) I have problem in getting the length of the checkbox object if the form contain only 1 checkbox object, wuth error "undefine", why???

    Pls advise....

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Since you have no script to it is not clear how you are getting the length.
    In general though, it is an input object with a type of checkbox.
    If you do
    Code:
    <script type="text/javascript">
    <!--
    window.onload = function () { alert(document.getElementsByTagName('input').length); }
    // -->
    </script>
    you will get 1 if there is only one input.
    You would have to check the input type to see if it is a checkbox.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If you have only one checkbox, there's no length property because it doesn't form an array.
    Code:
    var chks = document.forms[0].mycheckbox;
    if (chks.length){ //multiple checkbox
      for (var i=0; i<chks.length; i++){
         alert(chks[i].checked);
      }
    }
    else { //single checkbox
      alert(chks.checked);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you so much

    Yup, this is what I want!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •