Skip to content

Instantly share code, notes, and snippets.

@e-river
Last active February 14, 2017 07:10
Show Gist options
  • Save e-river/90dc8e57213db5ab5db709e0bba9eaf0 to your computer and use it in GitHub Desktop.
Save e-river/90dc8e57213db5ab5db709e0bba9eaf0 to your computer and use it in GitHub Desktop.
JavaScript for changing disabled to active button
(function(){
document.addEventListener('DOMContentLoaded', function(event) {
new ActiveForm().onEvent(event.target);
}, false);
})();
function ActiveForm() {
this.submit = document.getElementById('submit');
}
ActiveForm.prototype.init = function(target) {
this.onEvent(target);
};
ActiveForm.prototype.onEvent = function(target) {
var self = this;
target.addEventListener('keydown', function(e) {
self.active(e.target);
},false);
target.addEventListener('keyup', function(e) {
self.active(e.target);
},false);
target.addEventListener('keypress', function(e) {
self.active(e.target);
},false);
target.addEventListener('change', function(e) {
self.active(e.target);
},false);
};
ActiveForm.prototype.active = function(that) {
if (that.value.length > 0) {
this.removeAttr();
} else {
this.AddAttr();
}
};
ActiveForm.prototype.removeAttr = function() {
this.submit.removeAttribute('disabled');
};
ActiveForm.prototype.AddAttr = function() {
this.submit.setAttribute('disabled', 'disabled');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment