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?
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.
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.
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.
Syntax: $("TagId"). autocomplete({ source : itemList // List of Words. })
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','');
}
});
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 */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With