Created
December 13, 2012 19:53
-
-
Save iamdustan/4279244 to your computer and use it in GitHub Desktop.
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 ($) { | |
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); |
$('input.handleThatSubmitButtonLikeAChamp').autoSubmitHandler({ button: '#theButton' });
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And then it would be called with $.autoSubmitHandler('#inputName', '#buttonName')?