1

I want to select the first item but don't want to populate the input with it. So if I press enter the first value is selected but never does the input get autopopulated until the user either hits enter or clicks elsewhere on the screen causing the list to close.

I have had a look at selectFirst and autoFocus but they were not exactly what I was looking for. Not sure if what I am asking for is even possible but it seems to me that it must be.

Any ideas?

2 Answers 2

2

First off, selectFirst and autoFocus are not options on the jQueryUI autocomplete. They are from the plugin that eventually was adopted into jQueryUI and so the documentation found here is deprecated.

I'm not exactly clear on what you're trying to accomplish, but it sounds like you just don't want an item to populate the input element when you focus on it in the dropdown menu. This is indeed possible:

$("#autocomplete").autocomplete({
    source: [...],
    focus: function() {
        return false;
    }
}); 

returning false from the focus event handler will prevent the focused item in the menu from populating the dropdown.

Note that clicking elsewhere on the page won't populate the input element. This is because when you move your mouse away from the menu, an item you had selected loses focus. This seems like natural behavior to me, but there's probably a way to make that happen too.

Here's a working example: http://jsfiddle.net/andrewwhitaker/RyTuV/

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Andrew, Thanks for getting back to me. Sure I understand they are plugins and not part of the AC Ui original set of options. Although I feel that I have not made myself clear enough. When the list opens up I want the first option selected. I just do not want the input populated with the value. So if the user types in "soc" a few items will be listed like "Soccer", "Social Studies". So now with list open and "soc" in the input the user clicks away closing the list or hits enter closing the list. This is when the first option should populate the input.
Adding to that comment above I agree with your statement whereby it's not natural but it is what the client wants. So I kinda have to make it happen. Would appreciate any help with it.
0

You can use the focus event handler and autoFocus option together to populate a hidden input. Then when the autocomplete menu closes, use the close event handler to populate whatever field you want using the hidden input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.