Created
April 30, 2012 19:58
-
-
Save jbottigliero/2562233 to your computer and use it in GitHub Desktop.
jQuery Toggle Disabled
This file contains 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
<input id="test_input" type="text" value=""/> | |
<button id="disabler">Disable Input</button> | |
<script> | |
$('#disabler').on('click', function(){ | |
// No callback | |
// $('#test_input').toggleDisabled(); | |
// With callback | |
$('#test_input').toggleDisabled(function(el, disabled){ | |
if(disabled){ | |
el.value = 'DISABLED, YO!'; | |
} else { | |
el.value = ''; | |
} | |
}); | |
}); | |
</script> |
This file contains 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
(function($){ | |
$.fn.toggleDisabled = function(callback){ | |
return this.each(function(){ | |
var disabled, $this = $(this); | |
if($this.attr('disabled')){ | |
$this.removeAttr('disabled'); | |
disabled = false; | |
} else { | |
$this.attr('disabled', 'disabled'); | |
disabled = true; | |
} | |
if(callback && typeof callback === 'function'){ | |
callback(this, disabled); | |
} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment