number of checkboxes (array length) returned as undefined

R

reto

I get an 'undefined' object when trying to get the number of
checkboxes (with same name) in a form and there's only one checkbox.
However when there are two or more, I get the correct number.

<script>
function howmanyckboxes(){
alert(document.myform.mycheckbox.length);
}
</script>

-- HTML----

<form name="myform">
<input type="checkbox" name="mycheckbox" value=1>
<input type="checkbox" name="mycheckbox" value=2>
<input type="checkbox" name="mycheckbox" value=3>

</form>

Any ideas why the array length attribute reports 'undefined' when
there's one checkbox but correctly reports two or more?

Thanks
 
T

Thomas 'PointedEars' Lahn

reto said:
Subject: number of checkboxes (array length) returned as undefined

It is not an Array, but usually a NodeList.
I get an 'undefined' object when trying to get the number of
checkboxes (with same name) in a form and there's only one checkbox.
However when there are two or more, I get the correct number.

<script>

function howmanyckboxes(){
alert(document.myform.mycheckbox.length);
window.alert(document.forms["myform"].elements["mycheckbox"].length);

}
</script>

-- HTML----

<form name="myform">

The required `action' attribute is missing.

http://validator.w3.org/
<input type="checkbox" name="mycheckbox" value=1>

All attribute values should be quoted.
<input type="checkbox" name="mycheckbox" value=2>
<input type="checkbox" name="mycheckbox" value=3>

</form>

Any ideas why the array length attribute

As I said, there is no array. `length' is a _property_ of array objects,
but not exclusively.
reports 'undefined' when there's one checkbox but correctly reports two or more?

Because in that case it yields not a reference to a NodeList object but
one to an HTMLInputElement object. And the latter object does not have
a `length' property. So you can test for that:

var cb = document.forms["myform"].elements["mycheckbox"];
if (typeof cb.length != "undefined")
{
// multiple checkboxes with the same name
}
else
{
// only one checkbox
}

This was discussed here before; Google is your friend. [psf 6.1]


PointedEars
 
R

reto

Yes. Thanks. Without experts like you, amateur programmers would
indefinetely be stuck in bugs like this.

For anyone with my short experience, what Pointedears says is that:

The length was returned as 'undefined' because a single checkbox is
not an array. It only becomes an array when at least two checkboxes
have the same name (this change may occur when we create the
checkboxes dynamically and we may not know the number in advance). So
before looping for checked/unchecked, etc. we must first see if
there's a single checkbox or there's a number of checkboxes (array)
whose length is returned as a number.
 
T

Thomas 'PointedEars' Lahn

reto said:
Yes. Thanks

You're welcome, I think. Although you could have and should have quoted
what you are replying to, as recommended in the FAQ:

http://jibbering.com/faq/
Without experts like you, amateur programmers would
indefinetely be stuck in bugs like this.

Even amateur programmers, even script-kiddies, are capable of doing some
research first. Goodness knows how much copy-and-pray this newsgroup has
seen to date.
For anyone with my short experience, what Pointedears says is that:

It's quite weird of you to interpret my posting, obviously not understanding
it in the first place.
The length was returned as 'undefined' because a single checkbox is
not an array. It only becomes an array when at least two checkboxes
have the same name (this change may occur when we create the
checkboxes dynamically and we may not know the number in advance). So
before looping for checked/unchecked, etc. we must first see if
there's a single checkbox or there's a number of checkboxes (array)
whose length is returned as a number.

That may be better understandable by some, nevertheless it is hopelessly
incorrect. At least:

1. Read access to the `length' property of a checkbox (HTMLInputElement
object) with a unique name yields `undefined' because that object
simply does not have a `length' property (and all read accesses to
undefined properties yield the `undefined' value).

2. A checkbox is not and never becomes an array. Instead, when there
is more than one checkbox/radiobutton control with the same name in
a HTML form, that name can only be used to refer to a NodeList of
all checkboxes/radiobuttons in the same form with the same name.

3. As `length' is not a method (a function-property), it does not return
anything. It is a non-function property; it has a value, or IOW it
yields something on read access.

There is nothing that requires expert knowledge to understand my previous
answer or this answer; this is *basic* (DOM) knowledge. If you want to
continue oversimplifying like the above, and stay the script-kiddie that
you obviously are, you may do that in private.

Other people are here to learn something, and your providing them with your
simplistic view of the world does not help; in fact, more unlearning is then
required by them when they are confronted at some time with the problems
caused by the flaws in your explanation that they may have followed.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top