Created
September 4, 2013 05:23
-
-
Save alkaruno/6433039 to your computer and use it in GitHub Desktop.
Disable element(s), if field(s) is empty
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
/** | |
* 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