jQuery Custom Events

by b9chris

HTML

<input id=test value="Hit enter in this textbox" />

CSS

input {
    width: 100%;
}

JavaScript

(function($) {
    $.fn.enterKey = function(handler) {
        return this.keydown(function(ev) {
            if (ev.which == 13)
                handler.call(this, ev);
        });
    };
})(jQuery);

$('#test').enterKey(function(ev) {
    alert('You hit enter after typing: ' + this.value);
});