jquery autocomplete trigger dropdown on input:focus

柔情痞子 提交于 2019-11-30 09:39:58

Working demo http://jsfiddle.net/CNYCS/

Cool; so all you need to do is bind focus event with the autocomplete, rest `autocomplete will take over from there as you can see in the demo.

Helpful Link: http://forum.jquery.com/topic/how-to-bind-focus-input-to-trigger-autocomplete & http://docs.jquery.com/UI/Autocomplete#method-search

Hope this helps,

Rest code is in jsfiddle.

code

  $( "#tags" ).autocomplete({
        source: availableTags,
        minLength:0
    }).bind('focus', function(){ $(this).autocomplete("search"); } );

There is no obvious way to do so according to doc. But you can try with focus (or click or keyup) event on the autocomplete enabled textbox:

$('#autocomplete').trigger("keyup"); 

or

$('#autocomplete').trigger("focus"); 

or

$('#autocomplete').trigger("click"); 

As @Tats_innit mentioned the code, after that you need to just add the line

$('#tags').trigger("focus"); // as @Tats_innit's solution bind focus
                             // so you need to trigger focus

DEMO

Adding to the code given above. In my case availableTags were loading from server values. Hence I wanted to open popup only if the value was blank - this saves an unnecessary trigger for server to load tags again on focus.

$( "#tags" ).autocomplete({
        source: availableTags,
        minLength:0
    }).bind('focus', function () {
            if ($(this).val().length == 0) {
                $(this).autocomplete("search", "");
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!