0

I've got some autocompletes working finally, but there is one behavior I'd like to change. If the user types in a partial value and clicks anywhere outside of the list returned, the partial value gets set in the input. I would prefer that the value remain blank if the user doesn't select one of the returned options.

It looks like the focus event is what I should be modifying, but I find the API confusing... it says:

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.

but it doesn't give an example of how to cancel the event.

1 Answer 1

1

Found the answer in another question :) modifying the change event like so seems to do the trick:

$('#myID').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "cfc/autoSuggest.cfc?method=someMethod&returnformat=json",
                    dataType: "json",
                    data: {
                      search: request.term,
                      maxRows: 20
                    },
                    success: function(data) {
                      response(data);
                    }                   
                })
            },
            change: function(event, ui) {     
                if (!ui.item) {
                    $(this).val('');
                }
            }
        });
Sign up to request clarification or add additional context in comments.

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.