Is there way to extend jQuery to handle a custom enter key event?

Posted by Ken on Stack Overflow See other posts from Stack Overflow or by Ken
Published on 2010-03-26T17:44:01Z Indexed on 2010/03/26 17:53 UTC
Read the original article Hit count: 338

I write the following code all the time to handle when the enter key pressed:

    $("#selectorid").keypress(function (e) {
        if (e.keyCode == 13) {
            var targetType = e.originalTarget ? e.originalTarget.type.toLowerCase() : e.srcElement.tagName.toLowerCase();
            if (targetType != "textarea") {
                e.preventDefault();
                e.stopPropagation();
                // code to handler enter key pressed
            }
        }
    });

Is there a way to extend jQuery so that I could just write:

$("#selectorid").enterKeyPress(fn);

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery