Autofill: the first element is focused only when the user enters a tab key

Using jqueryUi autocomplete , I would like the first element to automatically focus only when the user types a tab key .
How do I complete this task?
Initializing autocomplete with the specified autoFocus option true does not match my purpose!

Any ideas?


Here is my code.
Please see Comment for more details.

  element.autocomplete({ minLength: 3, // the following option "autoFocus = true" // make the first item focused when the user type the first 3 letters // I would like the first item focused only when I type on TAB autoFocus: false, source: function (request, response) { // some code } }).data('autocomplete')._renderItem = renderItem; // the following piece of code works only when I type the first three letters, // If I type four letters and then tab it does not work! element.on('keyup', function (event) { if (event.keyCode === 9) { element.autocomplete( "option", "autoFocus", true ); } }); 
+1
source share
1 answer

Yes, autofocus will not do what you want. Instead, you can fake keystrokes that the user usually needs to do when they want to select an item.

 element.keydown(function(e){ if( e.keyCode != $.ui.keyCode.TAB ) return; // only pay attention to tabs e.keyCode = $.ui.keyCode.DOWN; // fake a down error press $(this).trigger(e); e.keyCode = $.ui.keyCode.ENTER; // fake select the item $(this).trigger(e); }); 

Demo: http://jsfiddle.net/uymYJ/8/

+2
source

All Articles