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
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Form object

    Hi,

    I'm encountering some problem with Form object.
    I have a form named KPIIN. In the form,
    I'm having 2 input field with type=text in the body.
    I gave the first textbox name=txtinput, id=txt1
    and the second textbox name=txtinput, id=txt2.

    In my body tag, I have onload=loodcookie() function.
    And in my function, I just want to alert a single line
    with

    alert(KPIIN.txtinput.length)

    So, when I run my page, I will received a popup returning
    2, which is the number of element in my body page. This is
    fine.

    However, when I take out one of the textbox and leave
    only a single textbox, this alert message return "Undefined"

    Should it returns "1"?

    Any help is greatly appreciated. Thanks in advance.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by nivla
    Should it returns "1"?
    No. When there's only one control with a particular name, there is no collection or array formed (unlike when there are two or more same named controls). That's why there's no length property.

    When there's only one field with that name, you access it without an index

    alert(document.KPIIN.txtinput.value)

    Unlike with a collection:

    alert(document.KPIIN.txtinput[0].value)
    alert(document.KPIIN.txtinput[1].value)

    Take note of the document object. Your direct access to form will only work with IE.

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks glenn,

    That's what I think so. But however, in my page, the textboxes are generated dynamically. I wanted to control these textboxes to that
    not all user can see the same number of them.

    Therefore, I need to get the count of textboxes on the screen to enable
    me to set the condition. In this case, I'm using the KPPIN.txtinput.length
    to return me the count of textboxes. So, if there's only one textbox in the
    screen, what is the way for me to know, if the KPIIN.txtinput.length is
    not applicable?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    var txt = document.KPIIN.txtinput;
    if (txt.length){ //length property is supported so it means there are 2 or more fields
       for (var i=0; i<txt.length; i++){
          alert(txt[i].value);
       }
    }
    else { //there's only one textbox
      alert(txt.value);
    }


  •  

    Posting Permissions

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