Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox - checking certain checkboxes

Status
Not open for further replies.

JKPeus

MIS
May 11, 2005
19
US
I have a form with similar structure to this:

What I would like is that when an option is selected the corresponding title checkbox is automatically checked (and if the option is unchecked the title becomes unchecked)

Also I would like to have a button that checks/unchecks all checkboxes in the form.

I have found a tutorial on how to do the second part but as I very little JavaScript knowledge I do not know how to alter it to achieve the first problem!

Many thanks in advance!
 
>the corresponding title checkbox
What is a checkbox's title here?
 
I don't like the naming convention there. There is no reason scripters make unnecessary problems to themselves for the things under their own controls. In particular the naming conventions...

Also, I don't know what to assume and what not. Here is a possible version. (Only checking, no unchecking...) The rest you have to figure out yourself from the hints as shown in this particular realization.
[tt]
<html>
<head>
<title>Form</title>
<script language="javascript">
function doit() {
var sig="";
var osel=document.getElementById("sel");
var cinput=document.getElementsByTagName("input");
for (var i=0;i<cinput.length;i++) {
if (cinput.type=="checkbox") {
if (cinput.name==osel.options[osel.selectedIndex].name) {
sig=cinput.name;
break;
}
}
}
for (var i=0;i<cinput.length;i++) {
if ((cinput.type=="checkbox")&&(cinput.name.indexOf(sig)!=-1)) {
cinput.checked=true;
}
}
}
</script>
</head>
<body>
<form ACTION="#">
<select id="sel" name="sel" onchange="doit()">
<option name="t0">-select a Title-</option>
<option name="t1">TITLE 1</option>
<option name="t2">TITLE 2</option>
<option name="t3">TITLE 3</option>
</select>
<br /><br />
<input type=CHECKBOX name="t1"><b>TITLE 1</b><br></br>
<input type=CHECKBOX name="t1_1">Option 1<br>
<input type=CHECKBOX name="t1_2">Option 2<br>
<input type=CHECKBOX name="t1_3">Option 3<br><br>
<input type=CHECKBOX name="t2"><b>TITLE 2</b><br>
<input type=CHECKBOX name="t2_4">Option 4<br>
<input type=CHECKBOX name="t2_5">Option 5<br>
<input type=CHECKBOX name="t2_6">Option 6<br><br>
<input type=CHECKBOX name="t3"><b>TITLE 3</b><br>
<input type=CHECKBOX name="t3_7">Option 7<br>
<input type=CHECKBOX name="t3_8">Option 8<br>
<input type=SUBMIT value="submit">
</form>
</body>
</html>
[/tt]
Naming convention here is absolutely arbitrary and just trying to let myself to get by, that's all. A correlation of some kind between select tag and checkbox subgroups is the necessary element to make it successful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top