Created
April 11, 2011 21:48
-
-
Save developish/914437 to your computer and use it in GitHub Desktop.
Simple way to bind the state of a checkbox to an associated function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bind a checkbox to a with checked and unchecked callback functions | |
// and run the appropriate action on page load: | |
// | |
// Examples | |
// | |
// $("input[type=checkbox][name=thecheckbox]").bindCheckbox({ | |
// checked: function() { | |
// alert("the checkbox is checked!") | |
// }, | |
// unchecked: function() { | |
// alert("the checkbox isn't checked!") | |
// } | |
// }) | |
// | |
// Returns chainable jQuery object | |
$.fn.bindCheckbox = function(options) { | |
return this.each(function() { | |
$(this).change(function() { | |
if ($(this).filter(':checked').val()) { | |
options.checked(); | |
} else { | |
options.unchecked(); | |
} | |
}).trigger('change'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment