I'm pretty late to the party, but I have an alternative, more performant solution to this. Since all that needs to be done is check the value of a CSS attribute, using a state variable and two event handlers to update said variable seems like a very heavy (and possibly brittle) solution. I feel that this style of coding is what makes parts of the javascript-driven web feel sluggish, even though we're provided with enormous computing power nowadays. But I digress.
You can test for the CSS display
attribute like this:
$(this).bind('click.ajaxselect', function(e) {
if($(this).autocomplete('widget')[0].style.display === 'none') {
$(this).autocomplete('search','');
}
});
For completeness, here's how to implement such a check in a "context-free" function:
function isSearchWindowOpen(id_of_input_element_the_autocomplete_is_bound_to) {
return $('#' + id_of_input_element_the_autocomplete_is_bound_to)
.data('ui-autocomplete') /* jquery's internal wrapper object */
.widget()[0] /* the actual search window DOM element */
.style.display === 'block'; /* standard display CSS attribute */
}
id_of_input_element_the_autocomplete_is_bound_to
contains any funny characters such as[
or.
. – Parliamentarianism