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!

Checkbox with 'All' functionality

Status
Not open for further replies.

ibike

Programmer
Apr 7, 2003
27
0
0
IT
Hi everybody,

I use check boxes inside my stylesheet. I'd like to add that kind of checkbox also if that one is checked in then all check boxes get 'checked' status.
Is somebody has an example or idea to do that?

Thank you for every help!
Ibi
 
You'd be better off asking this in a javascript forum, I can offer only a very simple script:
Code:
<script>
 function changeall(bValue) {
   document.formName.c2.checked = bValue;
   document.formName.c3.checked = bValue;
   document.formName.c4.checked = bValue;
   document.formName.c5.checked = bValue;
   document.formName.c6.checked = bValue;
   document.formName.c7.checked = bValue;
 }
</script>
<form name=&quot;formName&quot;>
 <input type=&quot;checkbox&quot; name=&quot;c1&quot; style=&quot;background: red;&quot; onClick=&quot;if (this.checked) { changeall(true) } else { changeall(false) }&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c2&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c3&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c4&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c5&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c6&quot; /><br />
 <input type=&quot;checkbox&quot; name=&quot;c7&quot; /><br />
</form>
Please note that form and input boxes must have a name so you can reference them in your script. If you need to have more flexible script, try using arrays for checkbox names. But that surely is a question for the Javascript forum: forum216
 
I found some javascript code once that'll select them all without having to specify individual check boxes. Here's the javascript:
Code:
function selectAll(formObj){
   for (var i=0;i < formObj.length;i++){
      fldObj = formObj.elements[i];
      if (fldObj.type == 'checkbox'){fldObj.checked = true;}
   }
}

Then, in your form, you have a button that calls the function:
Code:
<input type=&quot;button&quot; name=&quot;selectall&quot; value=&quot;Select All&quot; onclick=&quot;selectAll(this.form)&quot; />


Kevin
A+, Network+, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top