how prevent submit on press enter in twitter bootstrap typeahead plugin

paganotti picture paganotti · Nov 25, 2012 · Viewed 21.4k times · Source

I have a input text and I apply it typeahead plugin for suggesting items, but when I press enter key on input text it submit form.

How can I prevent form submit using twitter bootstrap typeahead plugin?

Answer

Andres Ilich picture Andres Ilich · Nov 25, 2012

You can target that specific input by adding an ID to it and simply removing the keydown event for enter like so:

JS

$(document).ready(function() {
  $('form input').keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});