Pixabay/jQuery-autoComplete

Destroy doesnt work?

kodeine opened this issue · 4 comments

Hello,
I am trying to make autosuggest work for country/state. Following works, but if i change "#autocomplete-27" from UK to US... it keeps populating the UK (first selected datasource). I tried using the destroy, but it doesnt work. Can anyone please help?

jQuery(document).ready(function(){

    jQuery("#autocomplete-27").attr({
        'disabled': 'true',
        'data-ajax': '',
    });

    // jQuery('#autocomplete-26').bind('change', function()
    jQuery('#autocomplete-26').on('keypress change blur', function()
    {
        var dataSource = jQuery("#autocomplete-27").attr('data-ajax');

        jQuery("#autocomplete-27").removeAttr('disabled');

        jQuery("#autocomplete-27").attr("data-ajax", function(i, origValue){
            var selectedValue = jQuery("#autocomplete-26").val();
            return 'https://domain.com/' + selectedValue + '.json';
        });

    });
    /// this does not work :/    
    jQuery("#autocomplete-27").autoComplete("destroy");
});

"Doesn't work" is a bit vague - what doesn't work exactly? Do you get an error?

Anyways, I guess the UK suggestions are populating the suggestions cache. You could try the autocompleter with disabled cache. Initialize it with cache: false. Maybe that helps.

@SimonSteinberger
Instead of destroy, i tried jQuery("#autocomplete-27").removeData(); and now it clears the old data.

Secondly, on states input i had to clear the autosuggestion div's, otherwise on country change, if i type US states, it would show that list even though the selected country is UK.
for this i used,

jQuery('#autocomplete-27').on('blur', function()
    {
        jQuery(".autocomplete-suggestions").remove();
    });

Thanks for a quick reply.

@kodeine That is not working for me.
Can u help me out....

Unable to clear the old data

A work around I am using is to reinitialize the drop down with an empty array as the source when I want to clear the contents, and then reinitialize again with the original source when I want the options to show.