...

View Full Version : Help with Form object



nivla
07-01-2005, 03:27 AM
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.

glenngv
07-01-2005, 04:25 AM
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.

nivla
07-01-2005, 05:11 AM
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?

glenngv
07-01-2005, 05:32 AM
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);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum