# jQuery UI Autocomplete

## Overview

You've seen functionality like Autocomplete before. Anytime you've done a search on Google for instance or typed in a url into your location bar of your browser. You would start to type and as you typed a narrowing list of options would be presented to you. This is the functionality that the Autocomplete plugin can provide you and your web site.

Autocomplete, added to an input field, enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.

By giving an Autocomplete field focus or entering something into it, the plugin starts searching for entries that match and displays a list of values to choose from. By entering more characters, the user can filter down the list to better matches.

This can be used to enter previous selected values, for example you could use Autocomplete for entering tags, to complete an address, you could enter a city name and get the zip code, or maybe enter email addresses from an address book.

You can pull data in from a local and/or a remote source: Local is good for small data sets (like an address book with 50 entries), remote is necessary for big data sets, like a database with hundreds or millions of entries to select from.

To customize the widget to work with these varied cases, you implement the source option. There are three variations:

- an Array with local data
- a String, specifying a URL
- a Callback

The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with a label or value property or both. The label property is displayed in the suggestion menu, the value will be inserted into the input element after the user selected something from the menu. If just one is specified, it will be used for both, eg. if you provide only value-properties, they will also be used as the label.

When a String is used, the Autocomplete plugin expects that string to point to a resource to return JSON data. It can be on the same host or on a different one (must provide JSONP). The data itself can be in the same format as the local data described above.

The third variation, the callback, provides the most flexibility, and can be used to connect any datasource to the Autocomplete. The callback gets two arguments:

- A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city autocomplete, term equals "new yo".
- A response callback, which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data (String-Array or Object-Array with label/value/both properties).

The demos all focus on different variations of the source-option - look for the one that matches your usecase, and take a look at the code.

### Dependencies

- UI Core
- UI Widget

### Example

- Demo
- View Source

A simple jQuery UI Autocomplete.

    $("input#autocomplete").autocomplete({
        source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
    });

    <!DOCTYPE html>
    <html>
    <head>
      <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
      <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
      <script type="text/javascript" src="http://jqueryui.com/latest/ui/jquery.ui.core.js"></script>
      <script type="text/javascript" src="http://jqueryui.com/latest/ui/jquery.ui.widget.js"></script>
      <script type="text/javascript" src="http://jqueryui.com/latest/ui/jquery.ui.autocomplete.js"></script>
      <script type="text/javascript">
      $(document).ready(function(){
        $("input#autocomplete").autocomplete({
        source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
    });
      });
      </script>
    </head>
    <body style="font-size:62.5%;">

    <input id="autocomplete" />

    </body>
    </html>

## Options

- ### delay

  Type:
    Integer
  Default:
    300

  The delay in milliseconds the autocomplete waits after a keystroke to activate itself. A zero-delay makes sense for local data (more responsive), but can produce a lot of load for remote data, while being less responsive.

  #### Code examples

  Initialize a autocomplete with the `delay` option specified.

        $('.selector').autocomplete({ delay: 0 });

  Get or set the `delay` option, after init.

        //getter
        var delay = $('.selector').autocomplete('option', 'delay');
        //setter
        $('.selector').autocomplete('option', 'delay', 0);

- ### minLength

  Type:
    Integer
  Default:
    1

  The minimum number of characters a user has to type before the autocomplete activates. Zero is useful for local data with just a few items. Should be increased when there are a lot of items, where a single character would match a few thousand items.

  #### Code examples

  Initialize a autocomplete with the `minLength` option specified.

        $('.selector').autocomplete({ minLength: 0 });

  Get or set the `minLength` option, after init.

        //getter
        var minLength = $('.selector').autocomplete('option', 'minLength');
        //setter
        $('.selector').autocomplete('option', 'minLength', 0);

- ### source

  Type:
    String, Array, Callback
  Default:
    none, must be specified

  Defines the data to use, must be specified. See Overview section for more details, and look at the various demos.

  #### Code examples

  Initialize a autocomplete with the `source` option specified.

        $('.selector').autocomplete({ source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] });

  Get or set the `source` option, after init.

        //getter
        var source = $('.selector').autocomplete('option', 'source');
        //setter
        $('.selector').autocomplete('option', 'source', ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]);

## Events

- ### search

  Type:
    autocompletesearch

  Before a request (source-option) is started, after minLength and delay are met. Can be cancelled (return false), then no request will be started and no items suggested.

  #### Code examples

  Supply a callback function to handle the `search` event as an init option.

        $('.selector').autocomplete({
           search: function(event, ui) { ... }
        });

  Bind to the `search` event by type: `autocompletesearch`.

        $('.selector').bind('autocompletesearch', function(event, ui) {
          ...
        });

- ### open

  Type:
    autocompleteopen

  After a request with the data ready, before it is actually displayed; also indicates the suggestion menu will be opened.

  #### Code examples

  Supply a callback function to handle the `open` event as an init option.

        $('.selector').autocomplete({
           open: function(event, ui) { ... }
        });

  Bind to the `open` event by type: `autocompleteopen`.

        $('.selector').bind('autocompleteopen', function(event, ui) {
          ...
        });

- ### focus

  Type:
    autocompletefocus

  Before focus is moved to an item (not selecting), ui.item refers to the focused item. The default action of focus is to replace the text field's value with the value of the focused item. Cancelling this event prevents the value from being updated, but does not prevent the menu item from being focused.

  #### Code examples

  Supply a callback function to handle the `focus` event as an init option.

        $('.selector').autocomplete({
           focus: function(event, ui) { ... }
        });

  Bind to the `focus` event by type: `autocompletefocus`.

        $('.selector').bind('autocompletefocus', function(event, ui) {
          ...
        });

- ### select

  Type:
    autocompleteselect

  Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item. Cancelling this event prevents the value from being updated, but does not prevent the menu from closing.

  #### Code examples

  Supply a callback function to handle the `select` event as an init option.

        $('.selector').autocomplete({
           select: function(event, ui) { ... }
        });

  Bind to the `select` event by type: `autocompleteselect`.

        $('.selector').bind('autocompleteselect', function(event, ui) {
          ...
        });

- ### close

  Type:
    autocompleteclose

  When the list is hidden - doesn't have to occur together with a change.

  #### Code examples

  Supply a callback function to handle the `close` event as an init option.

        $('.selector').autocomplete({
           close: function(event, ui) { ... }
        });

  Bind to the `close` event by type: `autocompleteclose`.

        $('.selector').bind('autocompleteclose', function(event, ui) {
          ...
        });

- ### change

  Type:
    autocompletechange

  After an item was selected; ui.item refers to the selected item. Always triggered after the close event.

  #### Code examples

  Supply a callback function to handle the `change` event as an init option.

        $('.selector').autocomplete({
           change: function(event, ui) { ... }
        });

  Bind to the `change` event by type: `autocompletechange`.

        $('.selector').bind('autocompletechange', function(event, ui) {
          ...
        });

## Methods

- ### destroy

  Signature:
    .autocomplete( 'destroy' )

  Remove the autocomplete functionality completely. This will return the element back to its pre-init state.

- ### disable

  Signature:
    .autocomplete( 'disable' )

  Disable the autocomplete.

- ### enable

  Signature:
    .autocomplete( 'enable' )

  Enable the autocomplete.

- ### option

  Signature:
    .autocomplete( 'option' , optionName , [value] )

  Get or set any autocomplete option. If no value is specified, will act as a getter.

- ### search

  Signature:
    .autocomplete( 'search' , [value] )

  Triggers a search event, which, when data is available, then will display the suggestions; can be used by a selectbox-like button to open the suggestions when clicked. If no value argument is specified, the current input's value is used. Can be called with an empty string and minLength: 0 to display all items.

- ### close

  Signature:
    .autocomplete( 'close' )

  Close the autocomplete menu. Useful in combination with the search method, to close the open menu.

## Theming

The jQuery UI Autocomplete plugin uses the jQuery UI CSS Framework to style its look and feel, including colors and background textures. We recommend using the ThemeRoller tool to create and download custom themes that are easy to build and maintain.

If a deeper level of customization is needed, there are widget-specific classes referenced within the ui.autocomplete.css stylesheet that can be modified. These classes are highlighed in bold below.

### Sample markup with jQuery UI CSS Framework classes

<input class="ui-autocomplete"/>

Note: This is a sample of markup generated by the autocomplete plugin, not markup you should use to create a autocomplete. The only markup needed for that is <input/>.