devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.57k stars 1.67k forks source link

Default result list #796

Closed sukiyanen85 closed 4 years ago

sukiyanen85 commented 4 years ago

I have an autocomplete input, and I want to show the user a default list of results when he click on the icon

image

Can't seem to find out how to achieve this through the documentation, any one have an idea?

tkirda commented 4 years ago

Autocomplete currently is triggered only when value changes. You may try this:

const instance = $('#autocomplete').autocomplete();

// Provide array of suggestions if not yet provided
instance.suggestions  = []; 

// Open suggestions:
instance.suggest();
sukiyanen85 commented 4 years ago

Thank you for taking the time to reply, this is my code at the moment:

const instance = $('#from').autocomplete({
        serviceUrl: '/autocomplete'
    });

    $('.autocomplete_location').click(function(){
        if(! $('#from').val().length){
            instance.suggestions = [];
            instance.suggest();
        }
    });

When I click on the icon, it shows the following error on the console

Uncaught TypeError: instance.suggest is not a function
tkirda commented 4 years ago

To get instance you need to call autocomplete() without parameters. Otherwise it returns jQuery object.