Stop 'enter' key submitting form when using jquery ui autocomplete widget

Mick Byrne picture Mick Byrne · Aug 30, 2011 · Viewed 14.5k times · Source

I've got a form that uses the jquery autocomplete UI plugin, http://jqueryui.com/demos/autocomplete/, which all works sweet except when you press the enter key to select an item in the autocomplete list, it submits the form.

I'm using this in a .NET webforms site, so there may be javascript handling associated with the form that .NET is injecting that is overriding the jQuery stuff (I'm speculating here).

Answer

mnsr picture mnsr · Aug 30, 2011

You can use an event handler on it.

$("#searchTextBox").keypress(function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) { //Enter keycode
        return false;
    }
});

see: jQuery Event Keypress: Which key was pressed?

also: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/javascript-char-codes-key-codes.aspx for list of keycodes