JQuery UI Autocomplete Detection

How can I determine if an input field is currently autocomplete jQuery UI? There seems to be no native method for this, but I hope there is something simple:

if ($("#q").autocomplete) { //Do something } 

However, this condition always returns true.

+7
jquery jquery-ui
source share
5 answers
 if ($("#q").hasClass("ac_input")) { // do something } 

UPDATE

The class name in the jQuery UI auto-complete widgets is now "ui-autocomplete-input", so the code will look like this:

 if ($("#q").hasClass("ui-autocomplete-input")) { // do something } 
+10
source share

You can also find the autocomplete behavior attached to the input element with the following line of code:

 if ($('Selector').data('autocomplete')) { } 
+9
source share

you can use

 if( $.isFunction( $.fn.autocomplete ) ){ } 

.isFunction is part of jQuery lib. (cite: http://james.padolsey.com/jquery/....isFunction )

+3
source share

This is true because after enabling js autocomplete, each $ () object now has a specific autocomplete () method (if you want to activate autocomplete for these elements). Your if () just says that this function is not null.

Unfortunately, I don’t have a system where I can check this (I left the laptop at home today), but I believe that autocomplete adds the css class name to the elements it uses. You can search for it.

+1
source share

If the jquery UI autocomplete plugin is already enabled for the page and you just want to check if a specific (input) element with autocomplete function has been configured, you can use the official 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 information can be found at http://api.jqueryui.com/autocomplete/#method-instance

+1
source share

All Articles