Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get jQuery Autocomplete to pop up on field focus? [duplicate]

Possible Duplicate:
jQuery autocomplete UI- I'd like it to start the search onfocus without the user having to type anything

jQuery UI Autocomplete

I want the options to appear as soon as my input is focused. Is there a setting for that? I tried setting minLength to 0, but it doesn't work... it still waits for a keypress.

like image 403
mpen Avatar asked Feb 07 '11 00:02

mpen


2 Answers

$("#yourField").bind('focus', function(){ $(this).autocomplete("search"); } );

Here a jsfiddle: http://jsfiddle.net/fpHf4/2/ Updated one (for IE): http://jsfiddle.net/q9ERL/4/

As enlighted by @HoldOffHunger you must also set minLength to 0

like image 130
Pierre Avatar answered Oct 31 '22 10:10

Pierre


I think u are breaking "autocomplete" utility just making a stylized select, thats the reason to wait for a keypress to have something to complete.

I know its not the anser u looking for, just remember this u trying to do just work with few options, if there are many u will get hard autocomple div load on firsts letters.

Or maybe u can have a 10 result records on ur sql query if is from this so get fast without loading all sort of results

--- I test focus bind on ie8, it fails, by the way its no a fail it does exactly what u want open div box on focus, the difference is that IE fires onFocus event whith jquery focus event, not like the others so su must evaluate on focus event if field its empty launch search , if is not just do nothing.. i hope this helps.

$("#yourField").bind('focus', function(){
  if($(this).val()!=""){ 
     $(this).autocomplete("search");
  } 
});
like image 42
Camilo Lizarazo Avatar answered Oct 31 '22 11:10

Camilo Lizarazo