16

I'm using the ui autocomplete: over here

I need the autocomplete to be open at all times, so it shouldn't close when somewhere in the body is clicked. I have googled this but I couldn't find anything.

Youss
  • 4,196
  • 12
  • 55
  • 109

4 Answers4

41

Hey sorry for the late response!

I feel this is a lot cleaner than keeping focus on the input and searching multiple times.

Try this...

JAVASCRIPT:

$( "#input1" ).autocomplete({
    source: availableTags,
    close : function (event, ui) {
        if (!$("ul.ui-autocomplete").is(":visible")) {
            $("ul.ui-autocomplete").show();
        }
    }
});

DEMO: http://jsfiddle.net/dirtyd77/AJtvJ/

Hope this helps!

Dom
  • 38,906
  • 12
  • 52
  • 81
16

here's a working example of how to do it : http://jsfiddle.net/4pk3V/

keep your autocomplete box open by searching the same input again whenever close event is triggered:

$( "#input" ).autocomplete({
    source: availableTags,

    close : function (event, ui) {
         val = $("#comment").val();
         $("#input").autocomplete( "search", val ); //keep autocomplete open by 
         //searching the same input again
         $("#input").focus();
        return false;  
    }
});
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • 2
    Removing `$("#comment").focus();` still keeps the autocomplete open and allows the user to actually select another element. – pete Feb 05 '12 at 12:26
8

There is also a CSS only option:

<style>
    .ui-autocomplete { display: inline-block !important; }
</style>

Example: http://jsfiddle.net/Monstermensch/G7YYZ/1/


You should also add the following code to avoid problems if the browser is resized (source: Repositioning jQuery UI Autocomplete on browser resize):

$(window).resize(function () {
    $("#input1").autocomplete("search");
});
Community
  • 1
  • 1
Simon Buechel
  • 150
  • 2
  • 6
  • The css only option is great. I use it to prevent the autocomplete widget from being closed while I am trying to inspect it in the inspector. – Kevin Lee Jul 14 '16 at 03:37
0

Here is an alternative solution: Add the following config to $input.autocomplete:

close: function (event, ui) {
  $input.autocomplete('widget').show();
}

In order to close the menu you need to add a click away handler. I found that the simplest way to do this is:

$input.on('blur', function () {
  $input.autocomplete('widget').hide();
});