jQueryUI Autocomplete - blur field after selection
10,615
Solution 1
You could use the 'close' method to call blur on the input field:
$("#season").autocomplete({
source: function(request, response){
$.getJSON("search.asp", {
term: request.term,
type: 'season'
}, response);
},
minLength: 0,
close: function(){
$(this).blur();
}}).focus(function(event, ui){
$(this).autocomplete("search", "");
});
Solution 2
Autocomplete has a select event, which is triggered when you select something from the dropdown list. In that event you can call .autocomplete('close') on your input to close the dropdown.
$("#season").autocomplete({
source: function(request, response){
$.getJSON("search.asp", {
term: request.term,
type: 'season'
}, response);
},
minLength: 0,
select: function(){
$(this).autocomplete('close');
}
}).focus(function(event, ui){
$(this).autocomplete("search", "");
});
Familiarizing yourself with the docs does wonders :)
http://jqueryui.com/demos/autocomplete/
The tabs below the example (options, events, methods, etc.), will give you all you need to know.
EDIT:
Try this, works for me but I only tested in Chrome, FF3 and IE8.
$("#season").autocomplete({
source: function(request, response){
$.getJSON("search.asp", {
term: request.term,
type: 'season'
}, response);
},
minLength: 0,
select: function(){
$('#season').autocomplete('close').blur();
}
}).click(function(event, ui){
$(this).autocomplete("search", "");
});
Typically, using click instead of focus isn't a great idea.
Author by
Jesse
Updated on July 31, 2022Comments
-
Jesse almost 2 years
I would like to be able to blur the field after I select a value from the dropdown. I currently have the autocomplete item searching on focus.
Here is what I have:
$("#season").autocomplete({ source: function( request, response ) { $.getJSON( "search.asp", { term: request.term, type: 'season' }, response );}, minLength: 0 }).focus(function(event, ui){ $(this).autocomplete("search",""); });
-
Jesse almost 13 yearsThat doesn't work. It still keeps the popup window, that displays the results, open.
-
Mechlar almost 13 yearsModified my answer. Blur doesn't close the dropdown, so we he to call .autocomplete('close') on it after select.
-
Jesse almost 13 yearsIt now closes and then reopens because the textbox retains focus after the insert of the value.
-
Jesse almost 13 yearsThat worked. Although, is there anyway to prevent the dropdown from flickering when the close event is triggered?
-
Mechlar almost 13 years@Jesse, I edited my answer again with a new snippet at the bottom.
-
Mechlar almost 13 years@Jesse: to get rid of that flicker you would need to not use the focus event. What is happening is when you select an item from the list it by defaults closes the dropdown and add focus back to your input, which in turn reopens the dropdown. using a click event instead of focus would solve this.
-
Jesse almost 13 yearsI ended up using the setTimeout function on the focus event with a 300 ms delay to prevent the flicker and called the following function... function showSearch(id) { if ($(id).val() == "") { $(id).autocomplete('search',''); } }
-
Uri over 12 yearsDidn't work for me. Neither did Dale's solution. The input does lose focus, but the options list remains for some reason.
-
M-Razavi about 4 yearsDidn't work for me, The input select the option, nothing wiriten in input. FF75