How To Select First Element in AutoComplete dropdown list
Asked Answered
C

6

8

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.

Curtcurtail answered 17/12, 2013 at 11:46 Comment(4)
what kind of autocomplete? jquery? bootstrap? can you add some code what youve tried so far?Griefstricken
Simply go to www.redbus.in and start enter a city name. Don't enter complete city name and go to next field.Even though if you don't enter complete city name automatically the first one will be selected. Like That I need.Thanks for your reply.Curtcurtail
I opened a similar question, as the answers suggested here did not work for me. See: #44592454Daunt
Possible duplicate of Select first jquery UI result automaticallyNepotism
N
3

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(); }
});
Nepotism answered 16/8, 2016 at 11:6 Comment(0)
P
1

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

Predator answered 17/12, 2013 at 11:57 Comment(0)
U
1

Just use the autoFocus option: http://bugs.jqueryui.com/ticket/7419

Unhook answered 6/3, 2014 at 20:17 Comment(0)
N
1

use

$('selector').autocomplete({selectFirst:true});
Nonperishable answered 16/6, 2015 at 12:11 Comment(1)
From where does this came from ? I cannot find any reference to selectFirst in jquery ui sources or documentation.Add
F
0

http://jqueryui.com/autocomplete/#custom-data

Check the example code in the above link. Hope this will help.

$( "#project" ).autocomplete({
  minLength: 0,
  source: projects,
  focus: function( event, ui ) {
    $( "#project" ).val( ui.item.label );
    return false;
  },
  select: function( event, ui ) {
    $( "#project" ).val( ui.item.label );
    $( "#project-id" ).val( ui.item.value );
    $( "#project-description" ).html( ui.item.desc );
    $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );
    return false;
 }

})

Frankish answered 17/12, 2013 at 12:7 Comment(0)
I
0

You have to just trigger select event of jquery to get first option selected.

$("#ElementID").autocomplete({
      source: availableList
 })._trigger('select');

You can find a detailed answer here: Language Lassi: Select first option using jQuery Autocomplete

Inexpressible answered 1/11, 2014 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.