Seems like there is no core config to setup a "Check All" checkbox unless I missed where it is. I'm creating a content type and have a taxonomy reference field and I have alot of terms, which means alot of boxes I have to check. Anyway to provide a checkbox to select everything?
5 Answers
Here is an updated version of @duckx code, it has the following improvements :
- only select the checkboxes of the label that has been clicked
- allows to toggle checkboxes
Drupal.behaviors.selectAll = {
attach: function (context, settings) {
$('body.page-node .form-type-checkboxes > label').click(function() {
$(this).nextAll('.form-checkboxes').find('input[type="checkbox"]').trigger('click');
});
}
};
-
-
I generally put this in a script.js inside my theme, don't forget to wrap it inside
(function ($) { ... })(jQuery);
as usual.– tostinniSep 27, 2013 at 6:47
You can do a form alter. hook_form_alter and attach your js to the form.
$form['#attached']['js'] = array(
drupal_get_path('module', 'YOURMODULE') . '/YOURMODULE.js',
);
Then add the code as in here
Here's a quick snippet for anyone looking for a quick check all functionality. It will basically make your field LABEL a check all button. No need to add any extra HTML elements or modules. This just works.
You can pretty much copy this into your js file and it will just work as i restrained it to working only on content Edit pages.
Obviously this isn't a long term solution, but mainly as a quick band aid patch.
jQuery(document).ready(function($) {
var $checkBoxes = $('input[type="checkbox"]');
$('body.page-node .form-type-checkboxes > label').click(function() {
$checkBoxes.attr('checked', true);
});
});
This contrib module appears to do the trick: https://www.drupal.org/project/checkall
There is no core config option for something like this, it would have to be JavaScript/jQuery (http://jsfiddle.net/PtMrM/).
<a href="#" class="check-all">Check All</a>
<label><input type="checkbox" name="ny"> New York</label>
<label><input type="checkbox" name="bos"> Boston</label>
<label><input type="checkbox" name="sfo"> San Francisco</label>
<label><input type="checkbox" name="dc"> Wasington DC</label>
<script type="text/javascript">
(function($) {
var $checkBoxes = $('input[type="checkbox"]');
$('.check-all').click(function() {
$checkBoxes.attr('checked', true);
});
}(jQuery))
</script>
Please note, that is very quick js, and won't be very user friendly (e.g., they click on it, there is no way to uncheck)