Search code examples
javascripteventsautocompleteonbluryui-autocomplete

How can I not react to blur event on YUI autocomplete select


I am trying to react diffrently to onBlur and onSelect events with YUI autocomplete. I need to hide the AC, when the user clicks somewhere else and leave it there on select. The problem is, that a blur event is fired onSelect as well. I could not find an event which is triggered only when clicking outside the AC and I am really stuck catching the blur event in some way in case of select since it is fired before the select event.

So basically what I want is an output like:

click outside ac: BLUR

select value: SELECT val

But what I am getting is:

click outside ac: BLUR

select value: BLUR, SELECT val

Which leads to a hidden AC on select.


Solution

  • Since the blur event fires when you select something, and it's likely that you won't be able to prevent that, on blur set a timeout. If the timer times out, then do what you would normally do on blur.

    In the select event, cancel the timer.

    Some very loose pseudo code:

    var blurTimer = null;
    
    function handleBlur() {
        blurTimer = setTimeout(function() {
            // do what you normally do onblur
        }, 50);
    }
    
    function handleSelect() {
        clearTimeout(blurTimer);
    
        // do what you normaly do on select
    }