type: widget
release: 1.8
status: complete
documentation: http://docs.jquery.com/UI/Autocomplete
demo: http://jqueryui.com/demos/autocomplete/
1 - Description:
An autocomplete widget helps entering data into text inputs by providing a list of suggetions based on a partial input. Similar behaviour is available in most browsers, but limited to data that the browser already knows about. This widget allows the application developer to provide application specific data, like results from a product search.
The autocomplete can be attached to a select-element, but that isn't the primary focus. For select elements with only a few items (less then 15), Selectmenu is the better choice. For selects with a lot of items, Autocomplete can make it easier to select elements.
If your input or textarea needs multiple (autocompleted) values, use ListBuilder, which builds upon Autocomplete.
The widget depends on Position to handle positioning of the suggestion list, as well as bgiframe.

2 - Visual Design:
3 - Functional Specifications/Requirements:
Common behaviors:
The list of suggestions...
- is displayed when the input has focus and at least one character (or more, depending on minLength option) is entered; when the input has a value and gets focus, another character has to be added to display the list
- is also displayed to display the list via a click on a double click (can be triggered with an additional arrow-down button), even without any value entered (and minLength: 0)
- updates on each keydown event
- a Tab keypress will always close the autocomplete and tab to the next field, and...
- insert the item that had focus in the suggestion list into the input
- do nothing when no item had focus
- a Enter keypress will select the selected item (preventing the default form submit) or do nothing special when there is no selected item (allow the default form submit)
- must be navigable with the keyboard:
- arrow down to move focus from the input to the first suggestion (then to the 2nd item with the next down arrow)
- page up/down to skip a page of items, depending on the size of the select in a scrollable list
- when nothing is selected, same behaviour as arrow up/down
- when something is selected, either stop at top or bottom edges, or scroll a full page, no matter if the first, last or any other item is focused on the current page
- from the first suggestion, arrow up to bring focus back to the input
- from the last suggestion, arrow down to bring focus back to the input
- arrow up from the input to focus last suggestion
- in other words, there are number of suggestions + 1 focusable items, where up/down rotates the focus, looping at the start and end
- whenever an item gets focus, its result-value is inserted into the textfield; when the focus goes back to the input, the original input value is restored (not mouse events)
- hide suggestions on escape
Datamodel
The autocomplete requires an array of objects (maps), containing two required properties:
- A label property to display in the autocomplete list ("John Doe <john@doe.com>").
- This can be HTML markup, if a seperate markup-free value is specified.

- A value property that ends up in the input field once selected ("john@doe.com");
- if undefined, same as label
- this must be plain-text (no markup)
Each object can have any amount of additional data associated, usually for filling in related field. This could include:
- an id property: a primary key in a database (65313, "FIN57984GFGH69FA")
- an index to map to an object (0, 1, ..., n)
- a shorthand like a country or province value ('US', 'NY')
If only an array of strings is provided, the widget will normalize that to an array of objects, using each string as both label and value. If only label is provided, its also used as value, and the other way round.
This data must be provided by the source-option.
Options:
- delay (number, default: 300)
- The delay in milliseconds the autocompleter waits after a keystroke to activate itself.
- should be set to something close to 0 for local datasources (eg. as part of the select-autocomplete plugin)
- minLength (number, default: 1)
- The minimum number of characters a user has to type before the autocompleter activates.
- source (various, default: none)
- a string
- gets interpreted as an asynchronous JSON source, with a GET parameter "term" containing the value
- the response must be a JSON array, containing either strings or objects with either a label or value properties, or both
- the response can contain any additional properties on each object
- an array
- gets intepreted as a local synchronouse source
- can be a string array, then each value is used as both label and value
- can be an array of objects, containing at least a label or value property; can contain any additional data
- a callback
- receives two arguments
- request is an object with a single term property and the current input as the value
- response is another callback, it expects the suggetions as the single argument
- can implement any kind of datasource
- use jQuery's $.ajax and customize error handling
- delegate to another library
- customize response, eg. highlighting the matched term in each label
Methods:
- 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 is specified, the current input's value is used
- call with an empty string and minLength: 0 to display all items
- close()
- close the autocomplete menu
- useful in combination with the search method, to close the open menu
Events:
- search
- before a request (source-option) is started, after minLength and delay are met
- can be cancelled
- open
- after the result is back, before it is actually displayed; also indicating the list to open
- 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
- 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
- on blur, when the input changed
- ui.item refers to the selected item, if one was selected from the suggestion list, else it's null
4 - Markup & Style:
4.1 Initial markup examples
<input type="text" />
4.2 Recommended transformed HTML markup
http://jquery-ui.googlecode.com/svn/trunk/autocomplete/tests/static/autocomplete/autocomplete.html
4.3 Accessibility recommendation
Not defined yet.
See http://www.w3.org/WAI/PF/aria/#aria-autocomplete
4.4 CSS & Theme
Found in markup (see 4.2).
In addition, while data is loaded, a class "ui-autocomplete-loading" is added to the input element. This can be used to display a loading indicator, eg. an animated gif.
5 - Latest version of plugin:
http://jquery-ui.googlecode.com/svn/trunk/tests/visual/autocomplete/default.html
Transforming a select into a combobox:
http://jquery-ui.googlecode.com/svn/trunk/demos/autocomplete/combobox.html
Using a jsonp webservice (via geonames.org):
http://jquery-ui.googlecode.com/svn/trunk/demos/autocomplete/remote-jsonp.html
6 - Open issues being discussed
Add a blur event since we have a focus event?
A cache option may eventually look like this:
- cache (boolean, default: true)
- if true, results will be cached when the returned resultset will only get smaller by further filtering
- if false, caching is disabled and new data is always requested
- One measure of figuring out if the result will only get smaller, or is still just a subset, is to compare the result size between two requests: When "foo" returns 50 results, and "foot" return 50 results, do another request for "foote".
When "foo" returns 50 results, and "foot" returns 45 results, use the local cache for anything further ("foote", "footer", etc.).
An implementation that doesn't require any configuration would be preferable. For now, anyone interested has to implement it himself using the source option, using this as a reference: http://jqueryui.com/demos/autocomplete/#remote-with-cache
Comments (13)
Scott González said
at 1:06 pm on Feb 1, 2010
A nice feature would be to animate the resizing of the menu like wikipedia does.
Johan Sjöberg said
at 5:46 am on Feb 22, 2010
The "select" event doesn't fire when selecting an item using the mouse. It instead fires when the search box loses focus due to a 'tab' keypress. Using jQuery 1.4.2, jQuery ui/menu/widget/autocomplete as given in the category-complete sample: http://jquery-ui.googlecode.com/svn-history/r3820/trunk/demos/autocomplete/categories.html.
Johan Sjöberg said
at 6:09 am on Feb 22, 2010
Actually, the above only occurs when a page is reloaded and the search-box is focused in the first place using $("searchbox").focus(); instead of by a manual mousepress.
Kenaniah Cerny said
at 12:13 am on Apr 20, 2010
IMHO, there's a few things missing from the spec:
* An option that requires inputs to be valid matches
* An option that specifies the maximum number of items displayed at a time
* The ability to invalidate a single term in the cache (or the entire cache)
* The ability to apply custom formatting to the item list via callback
* An option to enable / disable cases sensitivity (this might affect the cache)
Furthermore, it appears as though the autocompletechange event is broken as ui.item is never defined.
Scott González said
at 9:18 pm on Apr 20, 2010
This is all possible via the various options and events.
* An option that requires inputs to be valid matches
-- http://view.jqueryui.com/master/demos/autocomplete/combobox.html
* An option that specifies the maximum number of items displayed at a time
-- you can use the source option to limit the number of options that get returned.
* The ability to invalidate a single term in the cache (or the entire cache)
-- there is no built-in cache, you have to build that layer yourself. http://view.jqueryui.com/master/demos/autocomplete/remote-with-cache.html
* The ability to apply custom formatting to the item list via callback
-- http://view.jqueryui.com/master/demos/autocomplete/categories.html http://view.jqueryui.com/master/demos/autocomplete/custom-data.html
* An option to enable / disable cases sensitivity (this might affect the cache)
-- you can use a custom source option to accomplish this.
Arthur Borisow said
at 8:21 am on May 3, 2010
It would be nice to see option "size", which will define, how many results to show suggestions to show at once (using overflow)
Scott González said
at 9:14 am on May 3, 2010
See my comment directly above yours. You can already do this using the source option.
Anthony Potts said
at 7:34 am on May 12, 2010
You can limit the results in the source, but I believe what Arthur wants here is something which limits the number of menu-items that are displayed with a scrollbar on the right when the result set exceeds that amount.
Scott González said
at 8:35 am on May 12, 2010
We don't plan on supporting that. You can use CSS to specify a height if you want.
David Muir said
at 1:17 am on Jun 24, 2010
You need to specify max-height and overflow-y: auto;
There's also issues with the scrollbar reducing the horisontal width.
I got around it with some help from a little jquery plugin:
$.fn.overflows = function() {
var $this = $(this);
return {
overflow : ($this.innerHeight() !== $this.attr('scrollHeight')),
scrollOffset : ($this.innerWidth() - $this.attr('scrollWidth'))
};
};
And then added this to the open event:
open: function(){
var widget = $(this).autocomplete('widget'),
overflow = widget.overflows();
if(overflow.overflow){
widget.width(widget.width() + overflow.scrollOffset);
}
}
Anthony Potts said
at 7:30 am on May 12, 2010
I have noticed some oddness with the combobox example. Which is a shame because I was most excited about using it. First, initializing the combobox with a selected value proved challenging for some reason. Second, when selecting the value with the mouse, it doesn't keep the select in sync with the combobox in IE8 (possibly others). You can see this by opening the example and then selecting a value and then showing the underlying select. The select does not reflect the selected value.
Richard D. Worth said
at 7:46 am on May 12, 2010
Now that this plugin is in a stable release, discussion of use should occur on the forum http://forum.jquery.com/using-jquery-ui and bugs found should be reported here http://dev.jqueryui.com/newticket (note: registration required). Thanks.
CallMeLaNN said
at 9:06 pm on May 12, 2010
Now I try to create new widget Combobox based on the autocomplete because I also excited to use this UI. I take the selection into account. You can also help by creating the ticket to request or report a bug for Autocomplete as part of your excitement to use this jQuery UI to be more useful for us.
You don't have permission to comment on this page.