Skip to content

Instantly share code, notes, and snippets.

@bensochar
Last active August 29, 2015 13:56
Show Gist options
  • Save bensochar/9215365 to your computer and use it in GitHub Desktop.
Save bensochar/9215365 to your computer and use it in GitHub Desktop.
jQuery snippet to Detect Checkbox Changes
jQuery(document).ready(function($) {
$(':checkbox').checkedChange();
});
(function($) {
/*
* Detect Checkbox Changes
*/
$.fn.checkedChange = function (_class) {
_class = _class || "checked";
return this.each(function () {
var _label = $(this).closest("label.checkbox");
var _nme = this.name;
if(!navigator.userAgent.match(/MSIE/) ) {
$(this).change(function() {
$(this).is(":checked") ? _label.addClass(_class) : _label.removeClass(_class);
});
} else {
$(_label).click(function() {
$(':checkbox', this).is(":checked") ? _label.addClass(_class) : _label.removeClass(_class);
});
}
$(this).focus(function() {
$(_label).addClass("focus");
});
$(this).blur(function() {
$(_label).removeClass("focus");
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment