Chuck Anderson wrote:
Quote:
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 :-)
Quote:
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.