Thursday, February 17, 2011

Detecting jQuery UI autocomplete

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.

From stackoverflow
  • 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.

  • if ($("#q").hasClass("ac_input")) {
        // do something
    }
    
  • You can use

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

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

0 comments:

Post a Comment