Skip to content

Instantly share code, notes, and snippets.

@alkaruno
Created September 4, 2013 05:23
Show Gist options
  • Save alkaruno/6433039 to your computer and use it in GitHub Desktop.
Save alkaruno/6433039 to your computer and use it in GitHub Desktop.
Disable element(s), if field(s) is empty
/**
* Licensed under the MIT license by Alexey Karunos (https://github.com/alkaruno)
*/
(function ($) {
$.fn.disableIfEmpty = function (elementsToObserve) {
var elementToDisable, disableElement;
return this.each(function () {
elementToDisable = $(this);
disableElement = function () {
var hasEmptyField = false;
$(elementsToObserve).each(function () {
if ($(this).val().trim() == '') {
hasEmptyField = true;
return false;
}
return true;
});
elementToDisable.prop('disabled', hasEmptyField);
};
disableElement();
$(elementsToObserve).on('input', disableElement);
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment