Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  31] [ 1]  / answers: 1 / hits: 25257  / 12 Years ago, thu, december 20, 2012, 12:00:00

I'm working on a project with an autocomplete searchbox. Now I have the issue that I want to pass the value from the found autocompleteresults to the input box, but on the same time, I want the autocompletebox to hide when the inputfield is not more focused.



Now I have a conflict going on with both of them since the click on the autocompletebox is seen as focusout and hide the box even before it can pass the value. Any pointers or workarounds for this kind of issue? Here a jsfiddle to make it more clear to you.



http://jsfiddle.net/KeGvM/



Or here



CSS:



#a_c {display:none;}​


JS:



$('#search_field').focusout(function() {
$('#a_c').hide(); // IF I DELETE THIS IT WORKS
});

$('#search_field').focusin(function() {
$('#a_c').show();
});

$('#a_c a').click(function() {
$('#search_field').val('');
var value = $(this).text();
var input = $('#search_field');
input.val(input.val() + value);
$('#a_c').hide();
return false;
});​


HTML:



<input autocomplete=off onkeyup=searchFor(this.value); name=search id=search_field class=bold type=text placeholder=Search...>
<div id=a_c><a href=>hello world</a></div>​

More From » jquery

 Answers
135

My solution in the similar situation was using timeout to temporarily hold off the action taken in blur event handler. Like this:



$('#search_field').focusout(function() {
window.setTimeout(function() { $('#a_c').hide() }, 100);
});

[#81297] Wednesday, December 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;