473,511 Members | 16,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12276
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
21181
by: PhiSYS | last post by:
I want to know what's wrong with this code (I'm an amateur programmer). I'm trying to check if every field has a value or if checkboxes/radios have at least one item checked on each group (yes, you...
2
1890
by: Fred | last post by:
Hi, I defined a form consisting of checkboxes like: <form> <input type="checkbox" name=ck id=ck onclick="check(this.form)" <input type="checkbox" name=ck id=ck onclick="check(this.form)" ........
4
3023
by: David Jubinville | last post by:
Hi All, I recently came across an annoying problem and would greatly appreciate any help someone/anyone could offer. Here we go: 1. We have a 'Checklist' consisting of checkboxes and relating...
4
3379
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
2
9178
by: /.. | last post by:
Hi all, I'm working on a report display page that currently uses 5 checkboxlists with a total of 86 items to display values from 5 different tables in an Access database. The page works fine...
10
5181
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
0
4078
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
0
3090
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.