(Update: I changed the code slightly to use the official jQuery extend function for the plug-in definition, and to include the usage in the comments.)
/* * Exclusive Check Plugin v1.0.2 * Copyright (c) James Westgate * * @requires jQuery v1.3.2 * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.ph * http://www.gnu.org/licenses/gpl.html * * @usage $('input:checked').exclusiveCheck(); * @usage $('form input:checked').exclusiveCheck(); * @usage $('table tbody input:checked').exclusiveCheck(); * */ //Create closure (function($) { //Plugin definition $.fn.extend({ exclusiveCheck: function() { var selector = $(this); //Loop through each item in the matched set and apply event handlers return this.each(function(i) { //When the checkbox gets clicked, uncheck other checkboxes $(this).click(function(event) { var clicked = this; //Uncheck all except current if (this.checked) { selector.each(function() { if (this != clicked) this.checked = false; }); } }); }); } }); // end of closure })(jQuery);
2 comments:
Examples of use would be nice
There is a problem with the code: do not use $('input:checked')
you need to use:
$('input:checkbox')
Post a Comment