Pini
Pini

Reputation: 439

jQuery UI Autocomplete: How to make TAB select the first option on the list?

Using the jQuery UI autocomplete, is there any way to automatically select the first item on TAB click (or at least stimulate a down key)?

Upvotes: 1

Views: 508

Answers (1)

Sunil Kumar
Sunil Kumar

Reputation: 3242

You can use below jQuery for this:

$("#tags").on('keydown', function (e) {
                var keyCode = e.keyCode || e.which;
                if (keyCode == 9) {
                    //if alt pressed
                    e.preventDefault();
                    var _firstitem = $(".ui-autocomplete .ui-menu-item").first().text();
                    $(this).val(_firstitem);
                }
            });

Please have a look on this fiddle:

https://jsfiddle.net/mv38yozx/

Hope it will help you.

Upvotes: 1

Related Questions