So I'm scripting up a search field for my website. As the user types, a box appears below the field with search suggestions. The suggestions are keyboard navigable and clickable.
However, I'm having trouble getting the suggestions to disappear when the user click's elsewhere on the page. I've added an onBlur() handler to the search field, but when the user clicks on one of the suggestions, the blur happens before the click, thus making the suggestions box disappear and the click event happens in dead space instead of on the suggestion.
Here's what my HTML looks like:
<input type="text" id="search_field" onBlur="hideSuggestions()" />
<div id="suggestions_box">
<ul>
<li onClick="doSomething(1)">suggestion1</li>
<li onClick="doSomething(2)">suggestion2</li>
</ul>
</div>
So the question is, how can I hide the suggestions_box element when the focus leaves the search field, except when the new element with focus is the suggestions box?