I need a script that will select all the checkboxes in a dynamically created group whenever a radio button is selected. I could have from 1 to 5 radio buttons and up to 8 checkboxes. I might have output that looks something like this:
0 English 9
[] section 1
[] section 2
[] section 3
0 English 10
[] section 1
[] section 3
0 English AP 12
[] section 1
So, if the user clicks on English 9, then all the sections 1-3 below it are selected, then if they change their mind and click on English 10, then the English 9 checkboxes are cleared and the English 10 checkboxes are all checked.
Has anyone on this forum created a script like or similar to this, or seen one somewhere? Thanks…
Try it, you'll see what I'm talking about. BTW, I'm a long time programmer, so you can assume that I know a little of something about all this. However, I do pretty much stink at JavaScript. I started out as a COBOL programmer in the late '70's. Took a semi break to raise a family and get my degree. I got back in it about 5 years ago. Started by learning HTML, then progressed from there. Lots of new stuff to learn. I'm pretty good in PHP and fair in Perl and ASP, but JavaScript has been tougher for me for some reason. The class in a few weeks should help and maybe fill in some gaps in my understanding of the process.
<html>
<head>
<title>Untitled</title>
<script language="javascript">
<!--
function choose(myElement) {
for (var i = 0; i < document.forms[0].elements.length; i++) {
var currentName = document.forms[0].elements[i].name;
if (document.forms[0].elements[i].type == "checkbox") {
for (var x = 0; x < document.forms[0].elements[currentName].length; x++)
document.forms[0].elements[currentName][x].checked =
(document.forms[0].elements[i].name.split("-")[1] == myElement.name.split("-")[1]);
}
if (document.forms[0].elements[i].type == "radio")
document.forms[0].elements[currentName].checked =
(document.forms[0].elements[i].name.split("-")[1] == myElement.name.split("-")[1]);
}
}
//-->
</script>
</head>
<body>
<form>
<table border="0" cellpadding="1" cellspacing="0">
<tr>
<td><input type="Radio" name="c-1" value="Eng9" onClick="choose(this);"></td>
<td colspan="2">English 9</td>
</tr>
<tr><td>.</td><td><input type="Checkbox" name="s-1" value="Eng9-01A"></td><td>01A</td></tr>
<tr><td>.</td><td><input type="Checkbox" name="s-1" value="Eng9-02A"></td><td>02A</td></tr>
<tr><td>.</td><td><input type="Checkbox" name="s-1" value="Eng9-03A"></td><td>03A</td></tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td><input type="Radio" name="c-2" value="Eng10" onClick="choose(this);"></td>
<td colspan="2">English 10</td>
</tr>
<tr><td>.</td><td><input type="Checkbox" name="s-2" value="Eng10-01A"></td><td>01A</td></tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td><input type="Radio" name="c-3" value="Eng12AP" onClick="choose(this);"></td>
<td colspan="2">English 12 AP</td>
</tr>
<tr><td>.</td><td><input type="Checkbox" name="s-3" value="Eng12AP-01A"></td><td>01A</td></tr>
<tr><td>.</td><td><input type="Checkbox" name="s-3" value="Eng12AP-02A"></td><td>02A</td></tr>
</table>
</form>
</body>
</html>
[/quote]
I think that would fix it. I just don't know how to code it and I can't find it in my worthless book.
if (document.forms[0].elements[i].type == "checkbox") {
if (!document.forms[0].elements[currentName].length) { leng = 1; }
else { leng = document.forms[0].elements[currentName].length; }
alert ("length: "+leng); // leng getting set right here
for (var x = 0; x < leng; x++) {
document.forms[0].elements[currentName][x].checked =
(document.forms[0].elements[i].name.split("-")[1] == myElement.name.split("-")[1]);
}
}
[/quote]
[code=php]
<input type="Checkbox" name="s-1[]" value="Eng9-01A">
<input type="Checkbox" name="s-1[]" value="Eng9-02A">
<input type="Checkbox" name="s-1[]" value="Eng9-03A">[/code]
The name="s-1[]" tells php that this is an array coming in. So, if I add the [] to the end of the checkbox name, the JavaScript doesn't work anymore. I'm working on it, but the book I have isn't very good, and even if it was, I'm not sure how to look this one up. Any good book recommendations?0.1.9 — BETA 5.18