Wednesday, April 3, 2013

quickhit: jquery catch that enter key

Sometimes when you're making "fake" forms it makes sense to catch the enter key. Here's some code for that:

function catchEnter(jqo,funk){       
        jqo.keydown(function(e){
            var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
            if(key == 13) funk();
        });    

}

Usage, that would have an enter key in the username change focus to password, and enter key in password submit the login:

catchEnter($("#loginuser"),function(){ $("#loginpassword").focus(); });
catchEnter($("#loginpassword"),doLoginClick);

No comments:

Post a Comment