471,091 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,091 software developers and data experts.

checkbox.length confusion

I'm getting a bit confused, it seems a checkbox object only has a length
value if there is more than one checkbox of that name, 2 checkboxes named
'box' have a length of 2 but a single box returns null for length. Fine and
logical if the number of boxes is fixed or >2, a pain if the range is 1 or
more.

I need to check for a returned checkbox 'array' that might be zero, 1 or
many boxes. Is there a single call to detect 1 or more checkboxes with a
shared Name value?

Thanks

mark
Jul 20 '05 #1
6 12143
Mark Anderson wrote:
I'm getting a bit confused, it seems a checkbox object only has a length
value if there is more than one checkbox of that name,
A checkbox object does not have a `length' value/property. What you are
experiencing is, that if there are two or more elements of the same name
within a form, they are accessible by a collection of elements which has
indeed a `length' property.
I need to check for a returned checkbox 'array' that might be zero, 1 or
many boxes. Is there a single call to detect 1 or more checkboxes with a
shared Name value?


Well, you need to distinguish three cases:

A) There is no form element of that name within a form.
document.forms[...].elements['element_name'] is undefined.

B) There is only one form element of that name within a form.
document.forms[...].elements['element_name'] references this element.

C) There are two or more form elements of that name within a form.
document.forms[...].elements['element_name'] references the
collection of elements of that name, and
document.forms[...].elements['element_name'][numeric_index] references
a specific form element in that form.

Which results in something like

function getFormElements(oForm, sName)
{
if (oForm && oForm.elements)
{
var o = oForm.elements[sName];
if (o)
return o;
}
return null;
}

The function returns `null' if `oForm' is an invalid form object reference
or if there is no element in the form of the name sName. If only one element
if that name exists, a reference to it is returned. If there are two or more
elements of that name in the form, a reference to the collection is
returned. You then can use the index operator to reference a specific element:

<input type="button" ... onclick="var x = getFormElements(this.form, 'foo');
if (x) { if (x.length) x[0].value = 'bar'; else x.value = 'bar'; }" ...>
HTH

PointedEars

Jul 20 '05 #2
Thomas,

Many thanks. It is a little clearer. Meanwhile, I solved my problem (I don't
need as general a function as your example):

function anyCheck(theForm) {
if (theForm.RID) {
//checkbox 'RID' exists but 1 or many boxes?
if (theForm.RID.length) {
// >1 checkbox
var max = theForm.RID.length;
for (var idx = 0; idx < max; idx++) {
if (theForm.RID[idx].checked) {
//bail as soon/if we get a ticked box
return true;
}
}
}
else {
// only one checkbox
if (theForm.RID.checked) {
return true;
}
}
}
}

Then:

if (anyCheck(theForm)) {
// submit the form as the are selected checkboxes
// etc..

However, I'll note your more general example for when I next do this (and
can code from a new start).

Thanks again,

Mark


Jul 20 '05 #3
Mark Anderson wrote:
function anyCheck(theForm) {
[...]
Then:

if (anyCheck(theForm)) {
// submit the form as the are selected checkboxes
// etc..


I assume that `theForm' is a global variable. Because it is also
the identifier of the sole named argument of the function, it is
also a local variable within the function. Although possible, you
should avoid duplicate identifiers (in different scopes.)
PointedEars

Jul 20 '05 #4
Thomas,
I assume that 'theForm' is a global variable. <<<
Actually, neither. "theForm" is not declared as a global but simply as a
local in each function. I used the same name simply to remind me that a
preceding function needs the form reference required by anyCheck. IOW:

function masterCheck(theForm, this, that, other)
{
// general checks, then:
if anyCheck(theForm) {
//etc.

I could name them differently but without a global declaration they are
similarly named local variables (deliberately so). Or am I missing
something? (FWIW, the code works as intended). Perhaps this argues against
what I'm doing:Although possible, you should avoid duplicate identifiers (in different

scopes.)<<<

I can see that re-using a var name in several functions and mistakenly
declaring a global could cause incorrect parameter input. The flip side is
if a reference is passed through several nested/chained functions and having
to give the same thing a different name each time, e.g. theForm, aForm, frm,
myForm, etc.

Happy to be corrected if I'm misunderstanding something....

Thanks

Mark
Jul 20 '05 #5
Mark Anderson wrote:
I assume that 'theForm' is a global variable.
Actually, neither. "theForm" is not declared as a global but simply as a
local in each function. I used the same name simply to remind me that a
preceding function needs the form reference required by anyCheck. IOW:

function masterCheck(theForm, this, that, other)
{
// general checks, then:
if anyCheck(theForm) {
//etc.


ACK, that's acceptable.
Happy to be corrected if I'm misunderstanding something....


I see no problem in your way of naming because the local variable overlays
the previous one of the same name (lower in the stack) while the interpreter
is within the called method.

Only too many methods called from other methods calling other methods
*could* lead to a stack overflow (for suitable values of `many'.)
PointedEars

P.S.
Please don't use `>>> ... <<<' for quotes as this can confuse readers when
following different quote levels (I have corrected it here.) Also a short
attribution line to see easily who wrote what would be nice.

Jul 20 '05 #6
Thomas,
Only too many methods called from other methods calling other methods
*could* lead to a stack overflow (for suitable values of `many'.)


Thanks for the clarification.

Mark
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David Jubinville | last post: by
2 posts views Thread by /.. | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.