Toggle All Checkboxes on or off

Submitted by:David Villa

Date added:09 September, 2014


Simple Javascript snippet that lets you toggle a group of checkboxes on or off at the same time. This is the format for your HTML form.

Tags: checkboxes , toggle

Code Snippet:

FUNCTION toggleAll(id){
box = document.getElementById(id);

IF(box.checked == TRUE)
VAR setting = TRUE;
VAR setting = FALSE;

VAR cur_id = id+'1';

VAR count = 1;
WHILE(box = document.getElementById(cur_id))
box.checked = setting;
cur_id = id+count;

<input type=checkbox id='toggleall' onClick="toggleAll('myform');"> Toggle All

<input type=checkbox id='myform1'>
<input type=checkbox id='myform2'>
<input type=checkbox id='myform3'>