Sunday, November 17, 2013

Make "Enter" in Twitter Typeahead Select the First Item

This is just a quick post which may not be applicable for long but fixed the problem I had where I was trying to get the first item even if you hadn't selected it with the mouse or cursor.

$('input.typeahead').keypress(function (e) {
  if (e.which == 13) {
    var selectedValue = $('input.typeahead').data().ttView.dropdownView.getFirstSuggestion().datum.id;
    $("#input_id").val(selectedValue); $('form').submit(); return true;
  }
});

I appended the same information to this GitHub issue.

No comments: