:::: MENU ::::

Tuesday, January 20, 2009

keys = {
    getCharCode: function(evt){
            return (evt.charCode) ? evt.charCode :
                ((evt.which) ? evt.which : evt.keyCode);
    },
    isEnter:function(charCode){
            return (charCode == 13);
    },

    submitViaEnter:function(evt) {
            evt = (evt) ? evt : event;
            charCode = this.getCharCode(evt);
            if (this.isEnter(charCode)) {                   

                   //replace this with your own function:
                    form_submit();
                    return false;            
            }
            return true;
    }
}

To use it, add the following call to a text field:

onkeypress="return keys.submitViaEnter(event)"  

This code checks if a user has pressed the Enter key after finishing typing in the input field. If the key pressed is Enter, call the function that is normally invoked on the submit event. If the key pressed is not Enter, the function returns true and a user can continue typing.