Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Select First Element in AutoComplete dropdown list

Can any one help me how to select first element of autocomplete dropdown list if no element is selected? I tried with autoFocus. working for key board events. If I use mouse, the first element is not selecting which is auto focused.

like image 913
Srinivas Avatar asked Dec 17 '13 11:12

Srinivas


2 Answers

visit here for answer

$('#txt_search_city').autocomplete({
source: url,
delay: 0,
autoFocus: true,
select: function( event, ui ) {
    $( "#id_city" ).val( ui.item.id );
    $(this).closest('form').submit();
},
focus: function( event, ui ) { event.preventDefault(); }
});
like image 156
sandeep.gosavi Avatar answered Sep 22 '22 10:09

sandeep.gosavi


You can override the focus event to fill the textbox with the focused item's value:

$("#autocomplete2").autocomplete({
    // ...
    focus: function (event, ui) {
        $(this).val(ui.item.value);
    }
});

Demo here

like image 38
Salman A Avatar answered Sep 25 '22 10:09

Salman A