Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ui .autocomplete() not firing when same letter entered after blur()

Example code here: http://jsbin.com/etaziy (using http://jqueryui.com/demos/autocomplete/)

If you type in a 'j' you get the 3 names, 'John' 'Jack' and 'Joe'. If you then blur away, I clear the input, just by making the inputs val(''). Then if you go back to the input, and type a 'j' again, nothing happens? It's not until you type a second matching letter that you get the popup showing.

My example might seem a bit odd, but essentially this is a really cut back version of what I'm working on. I need to clear the input after a blur(), and clear it after a selection is made. Doing this is making subsequent selections look buggy.

I'm partly thinking this is intended functionality, but for my purpose, its not what i want. I really need the popup with the list to show as soon as any letter is typed.

Cheers.

like image 736
Eamonn Avatar asked Aug 17 '11 12:08

Eamonn


2 Answers

The problem is that the autocomplete keeps track of the value it is matching on internally, so when you first type j it sets this internal value to 'j' and then it wasn't being reset to the empty string when you changed the input to be empty.

After looking at the autocomplete source I wasn't able to find out how to directly access the internal variable, but you can force the autocomplete to update it for you by running another search once you've changed the input to be empty (and with a length of zero it won't actually do the search).

$(function() {
    $("#search").autocomplete({
                source: data,
                change: function() { // Triggered when the field is blurred, if the value has changed; ui.item refers to the selected item.
                        $("#search").val("");
                        $("#search").autocomplete("search", "");    //THIS IS THE NEW LINE THAT MAKES IT HAPPY
                }
        });
});
like image 141
Cebjyre Avatar answered Oct 26 '22 17:10

Cebjyre


Just replace line 98 in jquery.ui.autocomplete.js

if ( self.term != self.element.val() ) { // only search if the value has changed

with if(true){

like image 40
Vijaya Avatar answered Oct 26 '22 17:10

Vijaya