Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Autocomplete: Determine if search window is open?

I want to trigger a search in the onclick event of my input, but only if the search window isn't already open. Presently, I do this:

$(this).bind('click.ajaxselect', function(e) {
    if(!$(this).autocomplete('widget').is(':visible')) {
        $(this).autocomplete('search','');
    }
});

But I'm not overly fond of using the :visible selector because it searches up through all the parents as well. Is there some property I can check?

Dialog has this isOpen method, does autocomplete have something similar?

like image 850
mpen Avatar asked Jul 23 '11 22:07

mpen


People also ask

How do I know if autocomplete dropdown is open?

If autocomplete dropdown is open, Esc press shoult perform its default action only (closing dropdown and cancelling selection). How to check if autocomplete dropdown was opened ? It can check for any autocomplete box was opened in document body.

What is the default value of appendTo option of autocomplete () method?

Option - appendTo This option is used append an element to the menu. By default its value is null. When the value is null, the parents of the input field will be checked for a class of ui-front. If an element with the ui-front class is found, the menu will be appended to that element.

How does autocomplete work in jQuery?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

How can create autocomplete search box in jQuery?

Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })


2 Answers

Wouldn't be hard to set a simple variable:

$('.my_selector').bind('autocompleteopen', function(event, ui) {
    $(this).data('is_open',true);
});

$('.my_selector').bind('autocompleteclose', function(event, ui) {
    $(this).data('is_open',false);
});

Then your listener is easy:

$(this).bind('click.ajaxselect', function(e) {
    if(!$(this).data('is_open')) {
        $(this).autocomplete('search','');
    }
});
like image 50
AlienWebguy Avatar answered Oct 15 '22 11:10

AlienWebguy


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 */
}
like image 43
zb226 Avatar answered Oct 15 '22 11:10

zb226