5

I have jQuery autocomplete setup on an input box.

It is working fine.

However, I want to have a button that when clicked will trigger autocomplete as though the user type some text in the input. I would pass a specific string.

Any ideas on how I can do this?

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Anthony
  • 1,685
  • 4
  • 22
  • 29

2 Answers2

8

JQuery UI's autocomplete has a search method:

myAutocomplete.autocomplete('search', 'sometext');
sje397
  • 41,293
  • 8
  • 87
  • 103
  • 2
    this doesn't cause the drop down options to appear – Anthony Dec 16 '10 at 05:49
  • 2
    @Anthony...the docs say it should. Did you set `minLength:0`? – sje397 Dec 16 '10 at 06:01
  • In the options (i.e. the object you pass when creating the autocomplete) I would assume. I think that param just means 'don't search unless the input is this long' - if you haven't set it to something, it probably defaults to 0, so that's not likely to be the issue. – sje397 Dec 16 '10 at 06:39
  • 1
    @sje397 Your assumption was logical, but incorrect. minLength defaults to a value of 1, so leaving it unspecified can, indeed, cause an issue if your search string is empty. – jmbpiano May 01 '15 at 20:14
3

This worked for where #inputID is a text input on which the autocomplete is attached.

$("#inputID").autocomplete('search');

Tested in FireFox and Chrome (Ubuntu 12.04 LTS)

Luceos
  • 6,629
  • 1
  • 35
  • 65
Brooksie
  • 31
  • 1
  • Huh.. I can't get this to work. I have a text input with an ID and an autocomplete created on it, which works just fine (with remote datasource, v1.9). But if i try to do exactly the call like you mentioned (in a focus event, attached to my ID) - nothing happens. The event does happen, but the call to autocomplete('search') doesn't do anything, no drop down shows on the screen, no xHttp is made. I have no idea what else could be missing and I'm jumping around it for hours ;( – userfuser Jul 13 '15 at 19:33