views:

1650

answers:

3

Hey everyone I'm using jQuery's autocomplete in a relatively simple way:

$(document).ready(function() {
  var data = [ {text: "Choice 1"}, 
               {text: "Choice 2"}, 
               {text: "Choice 3"} ]

$("#example").autocomplete(data, {
  matchContains: true,
  minChars: 0,
  formatItem: function(item) 
    { return item.text; }
    }
  );
  });

How do I add an onclick event (like a button or a link) that will display all the available choices for the autocomplete? Basically I'm looking to make a hybrid of an autocomplete and a select/dropdown element.

Thanks!

+1  A: 

I can't see an obvious way to do that in the docs, but you try triggering the focus (or click) event on the autocomplete enabled textbox:

$('#myButton').click(function() {
   $('#autocomplete').trigger("focus"); //or "click", at least one should work
});
karim79
+1  A: 

$j(".auto_complete").focus(function() { $j(this).keydown(); })

Tom
I tried this, and it worked fine the first time, but after the textbox was focused and then unfocused, if i went back to it a second time it no longer showed the autocomplete list until i refreshed the page, at which point it would again work only once.
eidylon
$(".auto_complete").focus(function() { $(this).trigger("keydown") }) -> This works!
bortao
+2  A: 

You can trigger this event to show all of the options:

$("#example").autocomplete( "search", "" );

Or see the example in the link below. Looks like exactly what you want to do.

http://jqueryui.com/demos/autocomplete/#combobox

Mestek Webmaster
This worked PERFECTLY! I changed a little, and on my input element put `onfocus="javascript:$(this).autocomplete('search','');"` . Thanks for the help!
eidylon