View Full Version : Check/Select All?

10-27-2003, 08:38 PM
Hey is there a code I can use so that if i have a form with check boxes when i click like 'Check All' or something it will select every choice instead of the user having to select every option?

10-27-2003, 09:15 PM
Yes, there's several ways.

What would you prefer? A single checkbox that controls the checking/unchecking of all the others? Or a button that says "Check All", then when clicked says "Uncheck All".

Also, can I see the HTML of your form?

Willy Duitt
10-27-2003, 09:19 PM
Here's one way:

<script type="text/javascript">
<!-- Begin
function checkAll(field)
for (i = 0; i < field.length; i++)
field[i].checked = true ;

function uncheckAll(field)
for (i = 0; i < field.length; i++)
field[i].checked = false ;
// End -->

<form name="myform">
<input type="checkbox" name="list" value="1">1<br>
<input type="checkbox" name="list" value="2">2<br>
<input type="checkbox" name="list" value="3">3<br>
<input type="checkbox" name="list" value="4">4<br>
<input type="checkbox" name="list" value="5">5<br>

<input type="button" name="CheckAll" value="Check All"
<input type="button" name="UnCheckAll" value="Uncheck All"


10-27-2003, 11:31 PM
Hey Beetle :) i wanted it so it could just be another check box that when checked it would check every other box ya know? it could say check all or select all etc... :)

10-28-2003, 12:18 AM
Okay, here you go

<script type="text/javascript">

function checkAll( control, cbGroupName )
var cbGroup = control.form.elements[cbGroupName], i = 0, cb;
while( cb = cbGroup[i++] )
cb.checked = control.checked;


<input type="checkbox" onclick="checkAll( this, 'group1' );"/> Check all
<input type="checkbox" name="group1" value="1"/><br/>
<input type="checkbox" name="group1" value="2"/><br/>
<input type="checkbox" name="group1" value="3"/><br/>
<input type="checkbox" name="group1" value="4"/><br/>

</html>If the group will ever be only 1 checkbox, this function will need some additional code to prevent errors.