TheFrack
TheFrack

Reputation: 2873

Trigger jQuery Autocomplete manually

I'm using jQuery UI Autocomplete with some AJAX (the data isn't pulled until after they stop typing). I would like to make it so once the data is found, Autocomplete will then pop-up as a search result. This works, however only when I start typing again (the dropdown doesn't trigger until I type because it's not initialized until after I stop typing).

My code:

var availableTags = [
    "Perl",
    "PHP",
    "Python",
    "Ruby"
];
$('input#mainSearchBox').autocomplete({
    source: availableTags,
        minLength: 0
});
    $('input#mainSearchBox').data('autocomplete').menu.active;

The last part was an attempt to activate autocomplete, but it fails.

Upvotes: 20

Views: 39290

Answers (3)

Salma Gomaa
Salma Gomaa

Reputation: 1112

For https://github.com/devbridge/jQuery-Autocomplete, you can use:

$('input#mainSearchBox').autocomplete("getSuggestions", $('input#mainSearchBox').val())

Upvotes: 1

Thorsten
Thorsten

Reputation: 5644

The search method should do the trick:

$('input#mainSearchBox').autocomplete("search");

Fiddle

Upvotes: 53

jbabey
jbabey

Reputation: 46647

You can use the following script to toggle the autocomplete manually:

var textbox = $('input#mainSearchBox');
var autocompleteBox = textbox.autocomplete('widget');

// toggle the autocomplete widget
autocompleteBox.is(':hidden') ? 
    textbox.autocomplete('search', textbox.val()).focus() :
    autocompleteBox.hide();

This code can be found in the source of the combobox example on the jquery autocomplete demo site (lines 127-141).

Upvotes: 4

Related Questions