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

Checkboxes 3

Status
Not open for further replies.
Aug 18, 2003
111
GB
I have five checkboxes and each check box represents a category for example: software,hardware,stationery....etc

Each of these categories has a set of sub categories which all have a checkbox for example the hardware category has the following options: desktops,notebooks,servers,handhelds....

I have a function which will select all the options of a category when the category checkbox is clicked, so if hardware is ticked the desktops,notebooks,servers and handhelds are also ticked.

What I would now like is if the desktops checkbox is ticked then the hardware checkbox should also show as ticked and the remaining options will stay unticked (unless the hardware category is unticked which would remove all ticked options).

Does anyone know how to do this or point me in the right direction?

Thanks
 
how are you doing this part
I have a function which will select all the options of a category when the category checkbox is clicked, so if hardware is ticked the desktops,notebooks,servers and handhelds are also ticked.
- does this not help you, can you show some code.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
function handler(obj,catno) {
var cchkbox=document.getElementsByName(catno);
for (var i=0; i<cchkbox.length;i++) {
cchkbox.checked=obj.checked;
}
}

<input type="checkbox" name="cat" id="cat" onclick="handler(this,'sub10')" value="10">

<input type="checkbox" id="sub10" name="sub10" value="1">
<input type="checkbox" id="sub10" name="sub10" value="2">
<input type="checkbox" id="sub10" name="sub10" value="3">
<input type="checkbox" id="sub10" name="sub10" value="4">
 
which checkbox is for what sub category? you have them all named the same but also all with the same ID's which you cannot do. ID = unique IDentifier, this is not valid HTML.

to untick or tick relative to each other, you could use this..

Code:
<script>
function handler(obj,catno) {
   var cchkbox=document.getElementsByName(catno);
   for (var i=0; i<cchkbox.length;i++) {
    cchkbox[i].checked=obj.checked;
   }
}

function checktick(myname,target) {
   var cchkbox=document.getElementsByName(myname);
   var ticked = false;
   for (var i=0; i<cchkbox.length;i++) {
    if(cchkbox[i].checked){ticked=true;}
   }
   if(ticked){document.getElementById(target).checked=ticked;}
   else{document.getElementById(target).checked=false;}
}
</script>

<input type="checkbox" name="cat" id="cat" onclick="handler(this,'sub10')" value="10">

<input type="checkbox" id="sub10" name="sub10" value="1" onclick="checktick('sub10','cat')">
<input type="checkbox" id="sub10" name="sub10" value="2" onclick="checktick('sub10','cat')">
<input type="checkbox" id="sub10" name="sub10" value="3" onclick="checktick('sub10','cat')">
<input type="checkbox" id="sub10" name="sub10" value="4" onclick="checktick('sub10','cat')">


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
peterb,

can you show the HTML containing the checkboxes for one complete section (i.e., Hardware and all of it's subcategories)? once you provide this, we'll be of more help.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks this works. Your right about the id's i've now changed them to read cat10,cat20,cat30,..... for each of the main categories.
 
i don't suggest the method described above. it seems somewhat kludgy having to add those two variables to each function call...

if it works though, it works.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
how would you do it cory?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Is this the thread where you get stars for answering a question with a question?

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
hey Kaht, you know I give stars away on other peoples threads especially if i have learnt something from it, I might help people find solutions, but they are not always the BEST way to do things, so if any can expand my knowledge, they get a star from me everytime!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I was actually making reference to cory [lol]

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
yes but I gave cory the star for asking such a sensible question - lol ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
don't you just love a good old fashioned starfest!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top