checkbox.length confusion

M

Mark Anderson

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
 
T

Thomas 'PointedEars' Lahn

Mark said:
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
 
M

Mark Anderson

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
 
T

Thomas 'PointedEars' Lahn

Mark said:
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
 
M

Mark Anderson

Thomas,

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: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
 
T

Thomas 'PointedEars' Lahn

Mark said:
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.
 
M

Mark Anderson

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
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top