autocomplete: how do I avoid a duplicate search?

Posted by dnagirl on Stack Overflow See other posts from Stack Overflow or by dnagirl
Published on 2010-03-09T13:21:58Z Indexed on 2010/05/13 22:34 UTC
Read the original article Hit count: 218

Filed under:

I use JQuery plugin autocomplete as a kind of dataset chooser. If the user chooses a value from the autocomplete lookup, the database is queried for the matching dataset. If the user types in a new value, the user can enter a new dataset. An issue arises when the user types in an existing value rather than choosing it from the autocomplete lookup. When this is done, the autocomplete .result() method is not called and no dataset is retrieved. To fix this I added a .blur(function(){$(this).search();}); to the input element. This fixed the original problem.

Now I have the problem that .result() fires on selection from lookup AND on blur. I would like .result() to fire on selection from lookup OR on blur. How do I make that happen?

Here is my code:

$('#groupset').autocomplete('ajax/php/leeruns.php');
$('#groupset').result(
    function(event, data, formatted) {
        if(data){
            $('#groupsetdesc').val(formatted);
            groups.load(data[1]); //retrieve matching dataset
        } else {
            $('#groupsetdesc').val('');
        }
    }
).blur(function(){$(this).search();});

© Stack Overflow or respective owner

Related posts about jquery-autocomplete