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