I have this simple scenario (simplified for the sake of this question):
<input type="text" id="input">
<div id="autocomplete">.. some links..</div>
<script>
$('#input').bind('focus', function(){
$('#autocomplete').show();
}).bind('blur', function(){
$('#autocomplete').hide();
});
</script>
What I want to achieve is this:
- When user clicks in text input, autocomplete appears.
- When user clicks on anything in autocomplete DIV, it stays visible
- When user clicks anywhere outside of input and autocomplete then autocomplete disappears.
How can I achieve this ? Especially I am looking for an answer on the second point, because it must be easily achievable.
Thank you for your help in advance.
Correct me if I’m wrong, but I believe your code works for requirements #1 and #3, but not #2:
That’s because clicking on the div will cause the input field to lose focus, so the blur code would hide the div immediately. This means you can’t use the
blurevent for hiding the div. You could bind toclickondocument. From within the handler you’ll have to check if you should hide the div or not:I’m not sure if this will account for everything on your actual page, but I hope the idea is clear.