473,388 Members | 1,340 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,388 software developers and data experts.

Ranting: checkboxes are not placed in array.

Grrr,

Forgive my ranting, but this really irritated me a lot.

<ranting>
I have a page with X checkboxes with the same name.

code like:
var numberofCB = document.forms.updateentitiesform["bla"].length;

ok?

What happens to my surprise:
numberofCB now only contains the number of checkboxes with that name IF
there are more than 1.
Otherwise it is undefined.

I need that for stuff like:
for (var i=0; numberofCB<i ;i++){
document.forms.updateentitiesform["bla[]"][i].checked = false;
}

Which fails when there is only 1 checkbox.

I don't know who decided to start creating the array ONLY when more than 1
element is found, but that guy forces me to start coding stupid branches.

Why not always make it an array?

</ranting>

Thanks for your time.

Regards,
Erwin Moller
Jul 23 '05 #1
4 1325
Erwin Moller wrote:
Grrr,

Forgive my ranting, but this really irritated me a lot.

<ranting>
I have a page with X checkboxes with the same name.

code like:
var numberofCB = document.forms.updateentitiesform["bla"].length;

ok?

What happens to my surprise:
numberofCB now only contains the number of checkboxes with that name IF there are more than 1.
Otherwise it is undefined.

I need that for stuff like:
for (var i=0; numberofCB<i ;i++){
document.forms.updateentitiesform["bla[]"][i].checked = false;
}

Which fails when there is only 1 checkbox.

I don't know who decided to start creating the array ONLY when more than 1 element is found, but that guy forces me to start coding stupid branches.

Calm down a bit, and you'll see how silly that is.
refToForm.elementName and refToForm.elements.elementName only need one
variable slot to hold a reference to a single form control with a
unique name - why would an array be implemented? It's only needed for
multiple-same-named controls so, that's where you'll find it.
Why not always make it an array?


Because life is hard.

var el = document.forms.updateentitiesform.bla;
if (typeof el[0] != 'undefined')
{...got an array...

Jul 23 '05 #2
RobB wrote:
Erwin Moller wrote:
Grrr,

Forgive my ranting, but this really irritated me a lot.

<ranting>
I have a page with X checkboxes with the same name.

code like:
var numberofCB = document.forms.updateentitiesform["bla"].length;

ok?

What happens to my surprise:
numberofCB now only contains the number of checkboxes with that name IF
there are more than 1.
Otherwise it is undefined.

I need that for stuff like:
for (var i=0; numberofCB<i ;i++){
document.forms.updateentitiesform["bla[]"][i].checked = false;
}

Which fails when there is only 1 checkbox.

I don't know who decided to start creating the array ONLY when more

than 1
element is found, but that guy forces me to start coding stupid

branches.

Calm down a bit, and you'll see how silly that is.
refToForm.elementName and refToForm.elements.elementName only need one
variable slot to hold a reference to a single form control with a
unique name - why would an array be implemented? It's only needed for
multiple-same-named controls so, that's where you'll find it.


Sorry, but this gives me the penny-wise, poundfoolish feeling.
Because somebody wants to be smart and not use an array, the rest of the
world has to use branching in their javascript?

Does that make sense to you?

Really, this was a bad choice.

Why not always make it an array?
Because life is hard.


Maybe.
Another reason could be that the original creator was not thinking
everything through...

var el = document.forms.updateentitiesform.bla;
if (typeof el[0] != 'undefined')
{...got an array...


Yes, I know how to handle that.
That is unneeded code/branching in my humble opinion.

Really, I don't want to start ranting on you, but you make a stand for a bad
designdecision I think.

Thanks for your response anyway!
:-)
:-)
(Two smiley to make it up to you)

Regards,
Erwin Moller

Jul 23 '05 #3
Erwin Moller wrote:
RobB wrote:
Erwin Moller wrote:
Grrr,

Forgive my ranting, but this really irritated me a lot.

<ranting>
I have a page with X checkboxes with the same name.

code like:
var numberofCB = document.forms.updateentitiesform["bla"].length;

ok?

What happens to my surprise:
numberofCB now only contains the number of checkboxes with that
name IF
there are more than 1.
Otherwise it is undefined.

I need that for stuff like:
for (var i=0; numberofCB<i ;i++){
document.forms.updateentitiesform["bla[]"][i].checked = false;
}

Which fails when there is only 1 checkbox.

I don't know who decided to start creating the array ONLY when
more than 1
element is found, but that guy forces me to start coding stupid branches.

Calm down a bit, and you'll see how silly that is.
refToForm.elementName and refToForm.elements.elementName only need one variable slot to hold a reference to a single form control with a
unique name - why would an array be implemented? It's only needed for multiple-same-named controls so, that's where you'll find it.


Sorry, but this gives me the penny-wise, poundfoolish feeling.
Because somebody wants to be smart and not use an array, the rest of

the world has to use branching in their javascript?

Does that make sense to you?

Really, this was a bad choice.

Why not always make it an array?
Because life is hard.


Maybe.
Another reason could be that the original creator was not thinking
everything through...

var el = document.forms.updateentitiesform.bla;
if (typeof el[0] != 'undefined')
{...got an array...


Yes, I know how to handle that.
That is unneeded code/branching in my humble opinion.

Really, I don't want to start ranting on you, but you make a stand

for a bad designdecision I think.

Thanks for your response anyway!
:-)
:-)
(Two smiley to make it up to you)

Regards,
Erwin Moller


Here's the way I generally do it:

var f = document.forms.updateentitiesform,
ref = f.elements['bla[]'];
if (typeof ref[0] == 'undefined')
ref = [ref];
for (var i = 0, l = ref.length; i < l; i++)
ref[i].checked = false;

Jul 23 '05 #4
Erwin Moller wrote:
Sorry, but this gives me the penny-wise, poundfoolish feeling.
Because somebody wants to be smart and not use an array, the rest of
the world has to use branching in their javascript?


Rather, it's easier the way it is, IMO.

If an array was created every time for single form elements (since, you
know, _any_ type of form element can have multiple inputs with the same
name) then everyone's javascript code would look like

document.forms["formname"].elements["name"][0].value

Which is unnecessarill confusing for the 99% of the cases where there is
only a single input with the given name.

The best way to avoid branching every time you write your code is to use
generalized functions which give you the correct answer every time,
regardless of whether there are multiple inputs of the same name, if the
type of the input changes, etc.

At http://www.javascripttoolbox.com/validations/ I have getInputValue()
which will handle all these cases no matter what type of input it is, and
many people find that it makes life easier. Hope it helps.

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frank Collins | last post by:
Can anyone point me to some good examples on the web of using values from dynamically created checkboxes on forms in ASP, particularly relating to INSERTING those values into a SQL or Access...
8
by: Ralph Freshour | last post by:
I have multiple checkbox's created with an array name because I have many on the same web page - their names are like: frm_chk_delete frm_chk_delete frm_chk_delete frm_chk_delete etc. Here...
5
by: @(none) | last post by:
I have a page which is a set of CheckBoxes generated daily and thus the number of Checkboxes changes each day. What I want to do is allow the user to select one or more checkboxes and the push a...
6
by: terence.parker | last post by:
I currently have the following JS in my header: function checkall(thestate) { var checkboxes=eval("document.forms.EssayList.file_id") for (i=0;i<checkboxes.length;i++)...
5
by: Craig Lister | last post by:
Newish to c# - Coming from Delphi. I'd like to add 255 checkboxes to a screen at runtime, and name then cb1, cb2... cb255 The code below does not work, but, how can I get this to work? public...
3
by: Jaime Stuardo | last post by:
Hi all... I'm trying to use a custom validator for a group of checkboxes in order to validate that at least 1 checkbox is checked before submitting the form. I placed 2 checkbox controls in...
7
by: Srikanth Ram | last post by:
Hi, I'm creating a PHP application. In this a dynamic table with the fields in the database is generated in a page. I have placed a checkbox in each row of the table to approve/disapprove...
8
by: PeteOlcott | last post by:
http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_change_structure.html I would like to completely understand how GUI controls such as this one are constructed. In the...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.