Adding a select all checkbox to your drupal form.
Submitted by aaron on Mon, 05/04/2009 - 15:08
with terms
The following snippet can be useful in adding a "select all" box to your drupal forms (5.x - with or without jQuery update). Note that the code is careful to not check or uncheck any boxes that are not part of the immediate form.
//add javascript to the page to handle the select all checkbox
drupal_add_js("function toggleSelectAll(cbx){
if(\$(cbx).attr('checked') == true){
\$(\$(cbx).parents('form').get(0)).find('input[@type=checkbox]').each(function(){
if(!this.checked){
this.checked = true;
}
});
}
else{
\$(\$(cbx).parents('form').get(0)).find('input[@type=checkbox]').each(function(){
if(this.checked){
this.checked = false;
}
});
}
}
//Only show the select all box if javascript is working
\$(document).ready(function(){\$('#edit-select-all-0').show();});
",'inline');
//add the select all checkbox to the form
$form['select_all'] = array(
'#type' => 'checkboxes',
'#options' => array(0 => "Select All"),
'#attributes' => array('onchange' => "toggleSelectAll(this);", 'style' => 'display : none'),
);


