jQuery check all – Final update?

This may, or may not be, the final version of the check all function.
The main diffrence this time is that it’s accutally a jQuery function.
You simply enable it by wrapping a group of check boxes in som kind of box (fieldset, div, td, table, or anything else that feels good) and set a class on the wrapper.

<div class="checkbox-wrapper">
<input type="checkbox" name="box1" />
<input type="checkbox" name="box2" />
</div>
<script type="text/javascript">
$(document).ready(function(){
	$(".checkbox-wrapper").checkall();
});
</script>

Include this function:

/*
* jQuery function: checkall
* Description: Adds a check all box that will toggle the checkboxes within the parent element.
* Options:    text: The text that is displayed next to/the toggle box. Default is "Select/Deselect All"
*/
(function($){
$.fn.checkall = function(options){
var defaultoptions = {
text: "Select/Deselect All"
};
var options = $.extend(defaultoptions, options);
return this.each(function(){
var obj = $(this);
obj.append('<label><input type="checkbox" class="checkallselect" />&nbsp;'+options.text+'</label>');
$(obj.find('.checkallselect')).click(function()
{
obj.find('input[@type=checkbox]').attr('checked', $(this).is(':checked'));
});
});

};
})(jQuery);

Write Comment

CAPTCHA image


Comment Preview


#

2 Responses to jQuery check all – Final update? »

1
Comment by appel | 2009/01/08 at 01:40:54

Just for reference, there’s another way to do this:
http://lukerogers.net/2008/09/28/jquery-check-all-checkboxes-by-section/

2
http://blog.nordenfelt.com Comment by Nordenfelt | 2009/01/08 at 09:33:46

Hi appel, Thanks for the tip.

I actually found that example when I was trying to find a simple solution for this. What I didn’t like about it was that, at a glance, it wasn’t unobtrusive enough.
Essentially it does the same thing as my plug-in does but it requires a bit more hands on coding. I’d also prefer my script since it won’t be visible at all if you don’t have JS support.

10 Most Recent Twits

Loading twits...