Jerry Jerry - 4 months ago 14
jQuery Question

Autocomplete: How to focus on first item after pressing arrow down key on last item?

Focus on last item

Go through each option of the dropdown by using arrow down key. When focusing on the last option, and pressing arrow down key the cursor move to textbox instead of the first option. How make the cursor go to the first option?

Focus on first item

Answer

You can use below code block to fix that issue.

.autocomplete('instance')._move = function( direction, event ) {
    if ( !this.menu.element.is( ":visible" ) ) {
        this.search( null, event );
        return;
    }
    if ( this.menu.isFirstItem() && /^previous/.test( direction ) ||
         this.menu.isLastItem() && /^next/.test( direction ) ) {
        if ( !this.isMultiLine ) {
            this._value( this.term );
        }
        this.menu[ direction ]( event );
        // this.menu.blur();
        return;
    }
    this.menu[ direction ]( event );
}