My knowledge of JavaScript is limited. I learn from example and then
adapt those examples to suit my needs.
I have stumped myself on this one.
I have a form with checkboxes that I want to group by using a two
dimensional array.
<form name=msgs>
<input type=checkbox name=message[group1][1] value=1>
<input type=checkbox name=message[group1][2] value=1>
<input type=checkbox name=message[group1][3] value=1>
<input type=checkbox name=message[group2][1] value=1>
<input type=checkbox name=message[group2][2] value=1>
<input type=checkbox name=message[group3][1] value=1>
<input type=checkbox name=message[group3][2] value=1>
<input type=checkbox name=message[group3][3] value=1>
<input type=checkbox name=message[group3][4] value=1>
</form>
How do I access the checkboxes by group?
e.g.,
<a href="Javascript: SetAll('msgs', 'group1');">
Click to Select All (in group1)
</a>
<a href="Javascript: SetAll('msgs', 'group2');">
Click to Select All (in group2)
</a>
How do I make the SetAll work only on the checkboxes within one group?
Can I access the group of checkboxes by name, in order to set or clear
them all?
And in general, .....
How to I get the objects (length of the array, individual values?) of a
group of checkboxes?
Thanks for any help or insights.

Signature
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
> My knowledge of JavaScript is limited. I learn from example and then
> adapt those examples to suit my needs.
Often works better than a 1,000 page book :-)
> I have a form with checkboxes that I want to group by using a two
> dimensional array.
Array is a data structure with fixed element sequence where the
sequence is set by positive integer index value of each element.
This way it is not possible to call "milti-dimensional array" on:
..
message[group1][1]
message[group1][2]
...
What you have here is "message" object with properties "group1",
"group2" etc where each property is an object itself with single
property "1", "2" etc.
That is in relevance to the structure above; in attribute values
(below) these are just text strings "message[group1][1]" etc. - so the
distinction between enumerable object properties and array elements is
not so important: it is just a useful thing to remember for future use.
...
<input type=checkbox name=message[group1][1] value=1>
<input type=checkbox name=message[group1][2] value=1>
...
As these are just text strings, the straightforward way would be to
study these strings for a particular substring:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function selectGroup(frm, grp) {
var len = frm.elements.length;
var elm = null;
for (var i=0; i<len; ++i) {
elm = frm.elements[i];
if ((elm.name) && (elm.name.indexOf(grp) != -1)) {
elm.checked = true;
}
}
}
</script>
</head>
<body>
<form name=msgs>
<input type=checkbox name=message[group1][1] value=1>
<input type=checkbox name=message[group1][2] value=1>
<input type=checkbox name=message[group1][3] value=1>
<input type=checkbox name=message[group2][1] value=1>
<input type=checkbox name=message[group2][2] value=1>
<input type=checkbox name=message[group3][1] value=1>
<input type=checkbox name=message[group3][2] value=1>
<input type=checkbox name=message[group3][3] value=1>
<input type=checkbox name=message[group3][4] value=1>
<button type="button" onclick="
selectGroup(this.form, 'group1');
">Select group 1</button>
<button type="button" onclick="
selectGroup(this.form, 'group2');
">Select group 2</button>
</form>
</body>
</html>
On a "big long run" I would maybe use different classes for different
groups like
<input type=checkbox class="group2" name=message[group2][1] value=1>
and would compare with element.className in a loop.
That can be more elegant and effective ways around.
Chuck Anderson - 23 Oct 2006 04:29 GMT
>
>> My knowledge of JavaScript is limited. I learn from example and then
[quoted text clipped - 3 lines]
> Often works better than a 1,000 page book :-)
>
First off .... thanks for the great, detailed explanation and examples. ....
>> I have a form with checkboxes that I want to group by using a two
>> dimensional array.
[quoted text clipped - 12 lines]
> property "1", "2" etc.
>
.... snip
Thanks for explaining the above. You've given me further insight into
understanding JavaScript objects.
I'm still wrapping my head around all this information and will be
trying to put all this to use this tomorrow.
I really appreciate your help after I simply dropped in here like a
noob. I'll post back when I get this working.
> On a "big long run" I would maybe use different classes for different
> groups like
[quoted text clipped - 4 lines]
>
>
You've taught me a new one there - element.className. Thanks.

Signature
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
[snip]
> I have a form with checkboxes that I want to group by using a two
> dimensional array.
If check boxes are part of a logical group, then they should have the
same control name. The values then differentiate each member of that group.
> <form name=msgs>
Get into the habit of quoting attribute values. Many characters may not
occur unquoted, square brackets among them.
> <input type=checkbox name=message[group1][1] value=1>
> <input type=checkbox name=message[group1][2] value=1>
> <input type=checkbox name=message[group1][3] value=1>
<input name="message[group1]" type="checkbox" value="1">
<input name="message[group1]" type="checkbox" value="2">
<input name="message[group1]" type="checkbox" value="3">
To facilitate access server-side, you might need to alter the names. For
example, with PHP append a square bracket pair: "message[group1][]". If
any of the check boxes were successful, the $_GET super-global would
have an element with the name, message, which would contain an array.
This array would have an element with the name, group1, which would
contain another array. This array would be populated with the values of
the successful check boxes.
[snip]
> How do I access the checkboxes by group?
To access the group of controls above, one can use bracket notation.
Given a reference the form, formObj:
formObj.elements['message[group1]']
would yield a collection containing references to each of the three
controls. Clearly, the string value would need to updated to reflect the
real control names should you need to change it as discussed above.
> e.g.,
> <a href="Javascript: SetAll('msgs', 'group1');">
> Click to Select All (in group1)
> </a>
The javascript pseudo-scheme is a bad idea for reasons discussed in this
group in the past (use Google Groups to search for these conversations).
Preferably, you would use a button that was only included if scripting
was enabled.
[snip]
Hope that helps,
Mike
Chuck Anderson - 23 Oct 2006 04:35 GMT
> [snip]
>
[quoted text clipped - 13 lines]
> occur unquoted, square brackets among them.
>
Yeah, I've gotten into the habit of only using quotes around non
alphanumeric chars, which I forgot here.
>
>> <input type=checkbox name=message[group1][1] value=1>
[quoted text clipped - 8 lines]
> To facilitate access server-side, you might need to alter the names. For
> example, with PHP append a square bracket pair: "message[group1][]".
I am using this with Php, and that part is working. In fact, I have
altered my checkboxes to:
<input name="message[group1][]" type="checkbox" value="<?= $var ?>">
> [snip]
>
[quoted text clipped - 11 lines]
> real control names should you need to change it as discussed above.
>
This is the key. I'm making sense of this now.
>
>> e.g.,
[quoted text clipped - 8 lines]
> was enabled.
>
Never thought of that. Good point. Thanks for giving me some good
basic advice.
> [snip]
>
> Hope that helps,
> Mike
>
A lot. Thanks. I'll post back when I get my script working.

Signature
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Chuck Anderson - 30 Oct 2006 23:12 GMT
>
>>
[quoted text clipped - 86 lines]
>
>
So, .... sorry for the delay, but to follow up as I said I would ...
what I ended up doing was:
(btw; This is a Php/JavaScript application for reading all mailboxes at
a domain and displaying message headers and with a checkbox next to each
one to mark it for deletion)
... in a php loop ... (msg_num is an email message number)
<input type=checkbox name="message[<?= $domain ?>][]"
value=<?= $msg_num ?> style="vertical-align: middle;">
....
then
<input type=button value="Select All" class=plain_button
onClick="setall(this.form, 'message[<?= $domain ?>][]')";>
function setall(form, mbox)
{
var i=0;
var len = form.elements[mbox].length;
for(i=1 ; i<len ; i++) // was i=0;
{
form.elements[mbox][i].checked = true;
}
}
I was getting a JavaScript error (form.elements.mbox has no properties)
if I clicked on the setall button for a domain with no messages, so I added
<input type=hidden name="message[<?= $domain ?>][]" value=0> for every
domain
This seems like a kludge, but it was the only way I could think of to
fix it - and it works (the script that actually deletes the email
messages skips trying to delete message number 0. The first real
message is message number 1).
Thanks for the help.

Signature
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Michael Winter - 31 Oct 2006 02:19 GMT
[snip]
> <input type=button value="Select All" class=plain_button
> onClick="setall(this.form, 'message[<?= $domain ?>][]')";>
[quoted text clipped - 19 lines]
> This seems like a kludge, but it was the only way I could think of to
> fix it - and it works
Attempt to store the reference separately, and then test that it
actually references an object:
function setAll(form, mailbox) {
var group = form.elements[mailbox];
/* If there are no elements with that name, group will be
* null. */
if (group) {
/* The object, group, may actually refer to only one
* control. An input element doesn't have a length property
* but a collection does.
*/
if (!group.length) {group = [group];}
for (var i = 0, n = group.length; i < n; ++i) {
group[i].checked = true;
}
}
}
The check for single controls, rather than collections, is pretty weak
here as select elements do have length properties, potentially giving
false negatives. However, if the mailbox name will only ever apply to
one or more checkboxes, then the above should suffice.
> (the script that actually deletes the email messages skips trying to
> delete message number 0. The first real message is message number
> 1).
In the code above, it assumes zero once again, in anticipation of the
removal of that false input element.
Mike