Skip to content

Instantly share code, notes, and snippets.

@exside
Forked from eikes/jquery.uncheckable_radio.js
Created April 26, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exside/8890792b4c308b8b04962f5600603abd to your computer and use it in GitHub Desktop.
Save exside/8890792b4c308b8b04962f5600603abd 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment