JQuery user interface autocomplete: enter to complete

Using jQuery autocomplete , can the first element be automatically highlighted and filled when the user presses Enter, but not when they exit the input (input loses focus)?

The closest I found is this , but it autocomposts with focus loss.

+3
source share
1 answer

I think I have a solution that works - without breaking existing behavior - extending $.ui.autocomplete and overriding the method _suggest.

$.widget('ui.myAutocomplete', $.extend({}, $.ui.autocomplete.prototype, {
    _suggest : function(items) {
        // Call ui.autocomplete parent method
        $.ui.autocomplete.prototype._suggest.call(this, items);

        // Find the first list item
        var item = this.menu.element.children()[0];

        // Make this item active.
        // An event is expected so a fake one is passed as a parameter.
        this.menu.activate(new jQuery.Event('null.event'), $(item));
    }
}));

$.ui.myAutocomplete , $.ui.autocomplete

$(function() {
    var source = [
        'test 1'
        ,'test 2'
        ,'test 3'
    ];

    $('#selector')
        .myAutocomplete({
            source : source
        });
});
+1

Source: https://habr.com/ru/post/1782659/


All Articles