Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created December 13, 2012 19:53
Show Gist options
  • Save iamdustan/4279244 to your computer and use it in GitHub Desktop.
Save iamdustan/4279244 to your computer and use it in GitHub Desktop.
(function ($) {
function autoSubmitHandler ($input, $button) {
var self = this;
this.$input = this;
this.$button = $button;
this.$input.on('keydown', function (e) { self.handleInputKeydown(e); });
this.$input.on('blur', function (e) { self.handleInputBlur(e); });
return this;
}
autoSubmitHandler.prototype = {
isEmpty: function () {
return this.$input.val() == '' || this.$input.val() === this.$input.attr('placeholder');
},
handleInputKeydown: function(e) {
this.$button.removeAttr('disabled');
if (e.keyCode === 13 && this.isEmpty()) {
e.stop();
return false;
}
},
handleInputBlur: function () {
if (!this.isEmpty()) return false;
this.$button.attr('disabled', 'disabled');
}
};
$.ender({
autoSubmitHandler: function (options) {
return this.forEach(function (el) {
// something like this
$(el).data('autoSubmitHandler', new autoSubmitHandler(this, options.button));
});
}
}, true);
})(window.ender);
@rockbot
Copy link

rockbot commented Dec 13, 2012

And then it would be called with $.autoSubmitHandler('#inputName', '#buttonName')?

@iamdustan
Copy link
Author

$('input.handleThatSubmitButtonLikeAChamp').autoSubmitHandler({ button: '#theButton' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment