Skip to content

JavaScript Events

Mark Howells-Mead edited this page Oct 9, 2019 · 1 revision

Block form submission on enter key

Stops the default form submission when pressing the enter key whilst in a form field. In this example, the rule applies to search fields. This can be useful if the search happens amongst page elements, or via an AJAX autocomplete function.

$('input[type="search"]').on('keydown', function(event){
    if (event.keyCode == 13) {
        event.preventDefault();
        return false;
    }
});
Clone this wiki locally