Here’s a useful Javascript function I recently coded to allow a list of checkboxes in a form to be selected or unselected. The Check\uncheck function takes in two parameters, the first is the form name and the second is a boolean value – true or false, true being to select all checkboxes and false to un-select all.
<script language="javascript"> function checkAll(formname, checktoggle) { var checkboxes = new Array(); checkboxes = document[formname].getElementsByTagName('input'); for (var i=0; i<checkboxes.length; i++) { if (checkboxes[i].type == 'checkbox') { checkboxes[i].checked = checktoggle; } } } </script> |
The function gets the array of checkboxes from the selected form from their HTML tag ‘input’ type. As there are other input types such as ‘radio’, the function loops through all the input fields in the form, selecting only those that are checkboxes.
To call the function, add the following javascript command to your check all and uncheck all text or image links:
<a href="javascript:void();" onclick="javascript:checkAll('form3', true);">check all</a>
<a href="javascript:void();" onclick="javascript:checkAll('form3', false);">uncheck all</a> |
One of the advantages of using this method is that unlike some other similar functions, this check all works regardless of the name of the checkbox. There may be occasions for example where the checkbox name changes within a list, i.e. ‘checkbox1′, ‘checkbox2′ etc rather than ‘checkbox’ for the whole list.
The Javascript check all function has been tested for compatibility in IE8, IE9, Chrome & FireFox


WOW!! helped me thanks!!!
Remember to rename “form3″ in the script, according your form-name!
Wooow. Its worked for me. thanks for the code