Skip to content

Instantly share code, notes, and snippets.

@eikes
Created March 11, 2014 11:42
Show Gist options
  • Save eikes/9484101 to your computer and use it in GitHub Desktop.
Save eikes/9484101 to your computer and use it in GitHub Desktop.
jQuery plugin to allow unchecking radio buttons
(function ($) {
$.fn.uncheckableRadio = function () {
return this.each(function () {
var radio = this,
label = $('label[for="' + radio.id + '"]');
if (label.length === 0) {
label = $(radio).closest("label");
}
var label_radio = label.add(radio);
label_radio.mousedown(function () {
$(radio).data('wasChecked', radio.checked);
});
label_radio.click(function () {
if ($(radio).data('wasChecked')) {
radio.checked = false;
}
});
});
};
})(jQuery);
@kdimatteo
Copy link

This is great, added a bower repo for jquery-uncheckable-radio for prod. use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment