Autocomplete disallow free text entry?

According to the API documentation, the change event’s ui property is null if the entry was not chosen from the list, so you can disallow free text as simply as this:

$('#selector').autocomplete({
    source: url,
    minlength: 2,
    change: function(event, ui) {
        if (ui.item == null) {
          event.currentTarget.value=""; 
          event.currentTarget.focus();
        }
    }
});

Leave a Comment