Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocomplete - show the whole list

I have this code :

var myList = [ "Avellino", "Enna", "Frosinone" ];

myInput.autocomplete({
    source: function(request, response) {            
        var data = $.grep(myList, function(value) {
            return value.substring(0, request.term.length).toLowerCase() == request.term.toLowerCase();
        });            

        response(data);
    },        
    appendTo: "#myDiv"
});

and I'd like, when I click on the input box, show the list of all elements (with the same autocomplete box for choose value) of myList.

I suppose I need a third part handler, like :

myInput.focus(function () {

});

but I don't know how to dialogate with the autocomplete. Any ideas/solutions?

like image 955
markzzz Avatar asked Feb 22 '12 13:02

markzzz


1 Answers

@jasonlfunk is halfway there-- You have to call search on the autocomplete widget upon focus to get this to work:

var myList = [ "Avellino", "Enna", "Frosinone" ];

$('#myInput').autocomplete({
    minLength: 0,
    source: function(request, response) {            
        var data = $.grep(myList, function(value) {
            return value.substring(0, request.term.length).toLowerCase() == request.term.toLowerCase();
        });            

        response(data);
    }
}).focus(function () {
    $(this).autocomplete("search", "");
});

Example: http://jsfiddle.net/BRDBd/

like image 199
Andrew Whitaker Avatar answered Sep 20 '22 11:09

Andrew Whitaker