Skip to content

Instantly share code, notes, and snippets.

@sahibalejandro
Created September 9, 2014 02:16
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 sahibalejandro/09e33c8d02eb3cd787d7 to your computer and use it in GitHub Desktop.
Save sahibalejandro/09e33c8d02eb3cd787d7 to your computer and use it in GitHub Desktop.
Enable/disable form controls based on status of other checkbox/radio controls
/**
* "checkbox" style controls that enable/disable other controls
*/
$('[data-controls-controller]').on('click', function (e)
{
control_controller(this);
})
.each(function ()
{
if (this.checked) {
control_controller(this);
}
});
/**
* Enable/disable controls controlled by element.
*
* @param control
*/
function control_controller(control)
{
// Invert the disable/enable controls list based on the checked status of the control.
var controls_to_disable = $(control).data(control.checked ? 'disable-controls' : 'enable-controls');
var controls_to_enable = $(control).data(control.checked ? 'enable-controls' : 'disable-controls');
$(controls_to_disable).attr('disabled', 'disabled');
$(controls_to_enable).removeAttr('disabled');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment