Reputation: 7788
How can I detect whether or not an input box is currently a jQuery UI autocomplete? There doesn't seem to be a native method for this, but I'm hoping there is something simple like this:
if ($("#q").autocomplete)
{
//Do something
}
That conditional, however, seems to always return true.
Upvotes: 8
Views: 6784
Reputation: 359
If the autocomplete jquery UI plugin is already included for the page and you just want to check to see if a particular (input) element has been setup with autocomplete function, you can use the official API method as shown below:
if ($("#q").autocomplete("instance")) {
console.log("autocomplete already setup for #q");
} else {
console.log("NO autocomplete for #q");
}
More details can be found at http://api.jqueryui.com/autocomplete/#method-instance
Upvotes: 5
Reputation: 4023
if ($("#q").hasClass("ac_input")) {
// do something
}
UPDATE
The class name in the JQuery UI autocomplete widget is now 'ui-autocomplete-input' so that code would be:
if ($("#q").hasClass("ui-autocomplete-input")) {
// do something
}
Upvotes: 13
Reputation: 719
You can also find the autocomplete behavior attached to an input element by following line of code:
if ($('Selector').data('autocomplete')) {
}
Upvotes: 10
Reputation: 236
You can use
if( $.isFunction( $.fn.autocomplete ) ){ }
.isFunction is part of the jQuery lib. (cite: http://james.padolsey.com/jquery/....isFunction)
Upvotes: 3
Reputation: 103555
It's true because once you've included the autocomplete js, every $() object now has a autocomplete() method defined (in case you want to activate autocomplete for those elements). Your if() is just saying that that function is not null.
I, unfortunately don't have a system where I can check this (left the laptop home today), but I believe autocomplete adds a css class name to the elements it's using. You could look for that.
Upvotes: 1