473,776 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing two dimensional array checkbox values?

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="Javascrip t: SetAll('msgs', 'group1');">
Click to Select All (in group1)
</a>

<a href="Javascrip t: 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
6 10424
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>Untitl ed 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.le ngth;
var elm = null;
for (var i=0; i<len; ++i) {
elm = frm.elements[i];
if ((elm.name) && (elm.name.index Of(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(thi s.form, 'group1');
">Select group 1</button>

<button type="button" onclick="
selectGroup(thi s.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.classNa me in a loop.

That can be more elegant and effective ways around.

Oct 22 '06 #2
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.element s['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="Javascrip t: 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
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.classNa me in a loop.

That can be more elegant and effective ways around.

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

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
*************** **************
Oct 23 '06 #4
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.element s['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="Javascrip t: 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
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.element s['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="Javascrip t: 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="vertica l-align: middle;">
.....
then

<input type=button value="Select All" class=plain_but ton
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
Chuck Anderson wrote:

[snip]
<input type=button value="Select All" class=plain_but ton
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
17607
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like array_reverse(). Can anyone tell me why?
6
2698
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of deperation because when I defined it as char * insert(char table,int cols, char values)
1
1196
by: Carl Mercier | last post by:
Hi, I'm trying to create a multi-dimensional (2) array of CheckBox in VB.NET. Can't figure out the syntax... can anyone help? Thanks! here's my code:
3
1563
by: mtjarrett | last post by:
i having trouble accessing the values from superglobal arrays. there are two situations but i'm pretty sure it's the same problem. here's the deal: on page1.php i have several check boxes. before listing them i have the code $info = array(); and then each checkbox has
9
2094
by: Rainman | last post by:
In my HTML, I have several of the following: <input type='checkbox' name='right' id='right' value='0' /> All are the same except the value is set differently for each one. The reason for the is so I can access the checkbox values as an array on the processing page (when clicking 'Submit'); However, I want my Javascript code to examine these objects first. My onclick event handler function (below) is called (I get the 'hi there'
22
2295
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
8
11829
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
5
3887
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file. Step 2: Create a file called...
152
9911
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.