473,381 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 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 12260
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
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
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
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
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
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
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
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
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.