By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,979 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,979 IT Pros & Developers. It's quick & easy.

Accessing two dimensional array checkbox values?

P: n/a
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.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Oct 22 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
VK
Chuck Anderson wrote:
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.

Oct 22 '06 #2

P: n/a
Chuck Anderson wrote:

[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
Oct 22 '06 #3

P: n/a
VK wrote:
Chuck Anderson wrote:
>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 :-)
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.

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.
..... 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
<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.

You've taught me a new one there - element.className. Thanks.

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
*****************************
Oct 23 '06 #4

P: n/a
Michael Winter wrote:
Chuck Anderson wrote:

[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.
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>
<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][]".
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]

>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.
This is the key. I'm making sense of this now.
>
>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.
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.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Oct 23 '06 #5

P: n/a
Chuck Anderson wrote:
Michael Winter wrote:
>Chuck Anderson wrote:

[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.

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>
<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][]".
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]
>>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.

This is the key. I'm making sense of this now.
>>
>>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.

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.

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=0for 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.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*****************************
Oct 30 '06 #6

P: n/a
Chuck Anderson wrote:

[snip]
<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=0for
every domain

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
Oct 31 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.