.autocomplete()
Categories: UI
.autocomplete( [ options ] )
Plugin: jQuery.ui.autocomplete
Description: Apply the Autocomplete widget for each element in the set of matched elements
-
.autocomplete( [ options ] )
version added: 1.0options A map of additional options pass to the widget.
Autocomplete, when 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.
Autocomplete can be customized to work with various data sources, by just specifying the source option. A data source can be:
- 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 either 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 property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.
When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL. 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 data source to 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 field, the Autocomplete term will equal "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). It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.
The label is always treated as text, if you want the label to be treated as html you can use Scott González' html extension. The demos all focus on different variations of the source-option - look for the one that matches your use case, and take a look at the code.
-
create
This event is triggered when autocomplete is created.
-
search
Before a request (source-option) is started, after minLength and delay are met. Can be canceled (return false), then no request will be started and no items suggested.
-
open
Triggered when the suggestion menu is opened.
-
focus
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, though only if the focus event was triggered by a keyboard interaction. Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused.
-
select
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. Canceling this event prevents the value from being updated, but does not prevent the menu from closing.
-
close
When the list is hidden - doesn't have to occur together with a change.
-
change
Triggered when the field is blurred, if the value has changed; ui.item refers to the selected item.
-
destroy
-
.autocomplete( "destroy" )
version added: 1.0
Remove the autocomplete functionality completely. This will return the element back to its pre-init state.
-
-
disable
-
.autocomplete( "disable" )
version added: 1.0
Disable the autocomplete.
-
-
enable
-
.autocomplete( "enable" )
version added: 1.0
Enable the autocomplete.
-
-
option
-
.autocomplete( "option" , optionName , [value] )
version added: 1.0
Get or set any autocomplete option. If no value is specified, will act as a getter.
-
-
option
-
.autocomplete( "option" , options )
version added: 1.0
Set multiple autocomplete options at once by providing an options object.
-
-
widget
-
.autocomplete( "widget" )
version added: 1.0
Returns the .ui-autocomplete element.
-
-
search
-
.autocomplete( "search" , [value] )
version added: 1.0
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
-
.autocomplete( "close" )
version added: 1.0
Close the Autocomplete menu. Useful in combination with the search method, to close the open menu.
-