JQuery autocomplete: how to force selection from list (keyboard)

I am using JQuery UI autocomplete. Everything works as expected, but when I cycle with the up/down keys on the keyboard, I notice that the textbox is filled with items in the list as expected, but when I reach the end of the list and hit the down arrow one more time, the original term that I typed shows up, which basically allows the user to submit that entry.

My question: Is there a simple way to limit the selection to the items in the list, and remove the text in the input from the keyboard selection?

eg: if I have a list that contains {'Apples (AA)', 'Oranges (AAA)', 'Carrots (A)'}, if the user types 'app', I will automatically select the first item in the list ('Apples (AA)' here), but if the user presses the down arrow, 'app' shows up again in the textbox. How can I prevent that?



for force selection, you can use "change" event of Autocomplete

        var availableTags = [
            source: availableTags,
            change: function (event, ui) {
                    //http://api.jqueryui.com/autocomplete/#event-change -
                    // The item selected from the menu, if any. Otherwise the property is null
                    //so clear the item for force selection



Both of these other answers in combination work well.

Also, you can use the event.target to clear the text. This helps when you are adding auto-complete to multiple controls or when you don't want to type in the selector twice (maintainability issues there).

    source: availableTags,
    change: function (event, ui) {
    focus: function (event, ui) {
        return false;

It should be noted, however, that the even though the "focus" returns false, the up/down keys will still select the value. Cancelling this event only cancels the replacing of the text. So, "j", "down", "tab" will still select the first item that matches "j". It just won't show it in the control.

"Before focus is moved to an item (not selecting), ui.item refers to the focused item. The default action of focus is to replace the text field's value with the value of the focused item, though only if the focus event was triggered by a keyboard interaction. Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused."

reference On focus event:

focus: function(e, ui) {
    return false;

Define a variable

var inFocus = false; 

Add the following events to your input

.on('focus', function() {
    inFocus = true;
.on('blur', function() {
    inFocus = false;

And attach a keydown event to the window

        if(e.keyCode == 13 && inFocus) {

Need Your Help

How to insert tabs inside ion content in Ionic

javascript css ionic-framework tabs ionic-view

Always using the awesome framework Ionic for developing my mobile application. everything seems to work fine.

c++11 vector initialization in a function call

c++ vector c++11

I have a question about the new shortcut way of defining vectors in c++11. Suppose I have the following class