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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check/uncheck parent and child checkboxes

Status
Not open for further replies.

garry1

Programmer
Dec 6, 2002
49
0
0
US
When the parent checkbox is checked all the child check boxes will be checked.

My problem is how to uncheck child checkboxes when parent is unchecked.

Here is my code

<script>
<!--
function CA#chkTime#(){
for(i=0;i<document.myForm.atype.length;i++)
{
var myList = "#chknow#";
var listArray = myList.split(",");

for(j=0;j<listArray.length;j++){
if(document.myForm.atype.value == listArray[j]){
document.myForm.atype.checked = true;
break;
}
}
}
}
</script>

How to mod it so that clicking the parent box while checked will uncheck all child boxes.

Thanks.
 
Use these functions:

Code:
function doOnCheck(prnt, chld)
{
  for (var i=0;i<chld.length;i++)
  {
    chld[i].checked = prnt.checked;
  }
}

Pass it like this:

Code:
<form name="aform">
[red]<input type="checkbox" name="parent" onclick="doOnCheck(this,this.form.elements['child']);">[/red]Parent<br>
<input type="checkbox" name="child">Child1<br>
<input type="checkbox" name="child">Child2<br>
<input type="checkbox" name="child">Child3<br>
<input type="checkbox" name="child">Child4<br>
<input type="checkbox" name="child">Child5<br>
</form>

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
I have a form were I display parent and child checkboxs

Parent A checkbox as two child check box's.
Parent B checkbox as three child check box's

1. when user checks and submits two parent checkboxs (A and B) I would like to display a error message that "you can select from only from one parent check box".

2. when user selects child checkbox from Parent A and child checkbox from Parent B and clicks submit, I would like to display the same error message.

Thanks.
 
Would using radio buttons for the parents be a possibility? And disabling the children of the other when one is clicked?

--Chessbot
 
How do I make the parent checkbox to be checked when I check all the child check boxes in that parent group.
Thanks.
 
Code:
function allChecked(chks, prnt)
{
  if (!chks.length) // if not an array
  { 
    prnt.checked = chks.checked;
    return;
  }
  for (var i=0; i<chks.length; i++)
  {
    if (!chks[i].checked)
      return;
  }
  prnt.checked = true;
}

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
I applied the above script but something is not working. When I check all the child checkboxes in the group the parent checkbox is not getting checked. Here is my code.

<script language="JavaScript">
function allChecked(chks, prnt)
{
if (!chks.length) // if not an array
{
prnt.checked = chks.checked;
return;
}
for (var i=0; i<chks.length; i++)
{
if (!chks.checked)
return;
}
prnt.checked = true;
}
</script>

<form name="aform">
<input type="checkbox" name="parent" value="A" onclick="doOnCheck(this,this.form.elements['child']);">ParentA<br>
&nbsp;<input type="checkbox" name="child" value = "1" onclick="allChecked(this,parent);">Child1<br>
&nbsp;<input type="checkbox" name="child" value = "2" onclick="allChecked(this,parent);">Child2<br>
&nbsp;<input type="checkbox" name="child" value = "3" onclick="allChecked(this,parent);">Child3<br>

<input type="checkbox" name="parent" value = "B" onclick="doOnCheck(this,this.form.elements['child']);">ParentB<br>
&nbsp;<input type="checkbox" name="child" value = "4" onclick="allChecked(this,parent);">Child4<br>
&nbsp;<input type="checkbox" name="child" value = "5" onclick="allChecked(this,parent);">Child5<br>
</form>

Thanks.
 
Use
Code:
allChecked(this, [red]this.form.elements['parent'][/red]);

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
I changed as mentioned above but, its not working

<script language="JavaScript">
function allChecked(child, parent)
{
if (!child.length) // if not an array
{
parent.checked = child.checked;
alert(parent.checked)
return;
}
for (var i=0; i<child.length; i++)
{
if (!child.checked)
return;
}
parent.checked = true;
}
</script>

<form name="aform">
<input type="checkbox" name="parent" value="A" onclick="doOnCheck(this,this.form.elements['child']);">ParentA<br>
&nbsp;<input type="checkbox" name="child" value = "1" onclick="allChecked(this, this.form.elements['parent']);">Child1<br>
&nbsp;<input type="checkbox" name="child" value = "2" onclick="allChecked(this, this.form.elements['parent']);">Child2<br>
&nbsp;<input type="checkbox" name="child" value = "3" onclick="allChecked(this, this.form.elements['parent']);">Child3<br>

<input type="checkbox" name="parent" value = "B" onclick="doOnCheck(this,this.form.elements['child']);">ParentB<br>
&nbsp;<input type="checkbox" name="child" value = "4" onclick="allChecked(this, this.form.elements['parent']);">Child4<br>
&nbsp;<input type="checkbox" name="child" value = "5" onclick="allChecked(this, this.form.elements['parent']);">Child5<br>
</form>
 
[tt]parent[/tt] is a reserved keyword and that may be causing errors. How about
Code:
function allChecked(child, prnt)
{
  if (!child.length) // if not an array
  {
    prnt.checked = child.checked;  
    return;
  }
  for (var i=0; i<child.length; i++)
  {
    if (!child[i].checked)
      return;
  }
  prnt.checked = true;
}

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Is there an error? What happens?

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
There is no error.
When I check all the child checkboxes in a group it doesn’t check the parent checkbox.
 
Try changing the name of the two parents to parentA and parentB or something.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Hi chessbot, I use the same name for all the parent checkboxes. I display the results from a query. company names are the parent checkboxes and emp names are the child checkboxs. I group on a company name.
My problem is I want the parent checkbox to be checked automatically when the user checks all the child checkboxes in that parent group.

<input type="checkbox" name="parent" value="#company_name#" oncheck="doOnCheck(this,this.form.elements['child']);">#company_name#

<input type="checkbox" name="child" value="#employee_name#" ="doOnCheckChild(this,this.form.elements['parent']);">#employee_name#

<script>
function doOnCheckChild(){
for(i=0;i<document.myForm.parent.length;i++)
{
var myList = "#chknow#";
var listArray = myList.split(",");

for(j=0;j<listArray.length;j++){
if(document.myForm.parent.value == listArray[j]) {
document.myForm.parent.checked = true;
break;
}
}
}
}
</script>
 
You are going to have to link the parent and child in some way and give them unique names:
Code:
parent1
  child1a
  child1b
  child1c
parent2
  child2a
parent3
  child3a
  child3b
Something like that.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top