Jquery UI Autocomplete — Focus event via keyboard clears input content

If mousing over your autocomplete results correctly updates the input box with the currently highlighted value, but doing so via keyboard doesn’t (leaves the input blank) no matter what you put in your focus function, you need to prevent the default behavior from triggering.

For whatever reason, the default behavior works for mouse hovers but not keyboard.

        $("#faq_search").autocomplete({
            source: "/faq/ajax/",
            focus: function( event, ui ) {
                event.preventDefault(); // without this: keyboard movements reset the input to ''
                $(this).val(ui.item.question);
            },
            select: function( event, ui ) {
                document.location.href = ui.item.url;
            },
            context: this
        })

9 Comments

  1. Greg says:

    Thank you so much it worked perfectly! Saved me many hours of frustration I am sure. I just wish there was a way for the text to have a value when selected with arrows and auto filling the field. I was having a bit of a hard time debugging it. If you have any other tips on making it work better that would be great. Thanks for the perfect solution anyways 🙂

  2. hi, i am using jquery.autocomplet.js plugin in my application and facing same problem as i can not use the up and down arrow keys for selecting results from autocomplete list. What can i do to solve the problem.
    Thanks in advance
    rahul
    rahulmca.99@gmail.com

  3. Alberto says:

    Thanks, this worked like a charm on a Dynamics CMR 2013 :D.

  4. Rob says:

    Thanks. I’ve been trying different configurations but I didn’t think about the event.preventDefault().

    Thanks!!

  5. Jack Holt says:

    Thank you so much. Domo arigato gozaimasu

  6. bob says:

    Thanks Man

  7. Kelly says:

    Thank you so much for sharing this!! Saved me countless hours.

  8. Dario says:

    Old post, but it seems to never get outdated! Thanks a lot, this gave me a terrible headache!

  9. What is question in ui.item.question

Leave a reply to Rahul Srivastava Cancel reply