Display jquery ui auto-complete list on focus event
here is my code, anything wrong with it ? it doesn't seem to display list on focus, i still have to press a key before it displays list
<link media="all" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$('#id').autocomplete({
source: ["ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
],
minLength: 0
});
}).focus(function(){
$(this).trigger('keydown.autocomplete');
});
</script>
<input type="text" id="id">
Solution 1:
This directly call search method with default value when focus.
http://jsfiddle.net/steelywing/ubugC/
$("input").autocomplete({
source: ["Apple", "Boy", "Cat"],
minLength: 0,
}).focus(function () {
$(this).autocomplete("search");
});
Solution 2:
Looks like you are attaching your focus()
handler to the anonymous function and not the text box.
Try this:
<script type="text/javascript">
$(function() {
$('#id').autocomplete({
source: ["ActionScript",
/* ... */
],
minLength: 0
}).focus(function(){
// The following works only once.
// $(this).trigger('keydown.autocomplete');
// As suggested by digitalPBK, works multiple times
// $(this).data("autocomplete").search($(this).val());
// As noted by Jonny in his answer, with newer versions use uiAutocomplete
$(this).data("uiAutocomplete").search($(this).val());
});
});
</script>