The Config Object initializes the AutoComplete widget.
You define your List Objects - which control which options are available to the user - on the lists
property.
As a shorthand method, you can provide an array of Option Objects to the config object and it will be expanded as the default list for the widget. See the Simple List Example.
As another shorthand method, you can provide a single string value and AutoComplete assumes it's an AJAX url. See the Simple AJAX example.
Property / Type | Required | Default | Description | Example |
---|---|---|---|---|
String |
sometimes | n/a |
The It's value should be the name of a List Object.
|
|
Array of |
no | n/a |
If provided, sets the initial value of the AutoComplete widget. |
|
Object of |
yes | n/a |
The
|
|
Number or |
no | false |
The maximum number of Token Groups allowed in the search bar. Set |
|
Function |
no | n/a |
Function to execute when the value of the widget changes. The first argument to the function is the new value of the widget, the second argument is the old value of the widget. If this function returns a valid widget value, then that value becomes the value of the widget. If not, the original new value becomes the value of the widget. |
|
String |
no | '' |
A placeholder to show in the AutoComplete widget when there are no Token Groups. |
|
|
no | n/a |
Every error in AutoComplete has a unique code to help diagnose problems and search for solutions. If If If If |
|
String or |
no | ':' |
The HTML string to put between Tokens in the search bar. If The function should return an HTML string. |
List Objects are the heart of the AutoComplete widget. They define the options available to the user when they are typing.
The options for a List Object can be sourced directly in the JavaScript or externally with AJAX.
You can define the list workflow using the children
property on List Objects.
As a shorthand method, you can provide an array of Option Objects anywhere that expects a full List Object. See the Nested Lists example.
As another shorthand method, you can provide a single string value and AutoComplete assumes it's an AJAX url. See the Nested Lists with AJAX example.
Property / Type | Required | Default | Description | Example |
---|---|---|---|---|
String or |
no | 'AJAX Error' |
An HTML string to display when an AJAX request has failed. If Possible values for the first argument are: The function should return an HTML string. |
|
String or |
no | 'Searching |
An HTML string to display when searching for options. If The function should return an HTML string. |
|
Object or |
no | n/a |
Internally, AutoComplete uses the jQuery If provided, You can override any If The function should return an object to pass to |
|
Function |
no | n/a |
The first argument is the data returned by the server, the second argument is the text that the user has entered, and the third argument is the current value of the AutoComplete widget. The function should return an array of Option Objects.
|
|
String or |
no | n/a |
The url to send an AJAX request to. The string The text will be escaped using If The function should return a url string. |
|
Boolean |
no | false |
If If There can still be options when |
|
Boolean |
no | false |
If If Only HTTP GET requests are cached. If the browser does not support localStorage it will only cache results for the duration of the page. If you need to support browsers that do not have localStorage I recommend using a polyfill. |
|
Number or |
no | 1209600 (2 weeks) |
Length of time in seconds to keep results in the cache. Set
|
|
String |
no | n/a |
When any option from the list is selected from the dropdown, If there is a |
|
Boolean |
no | true |
If This option programmatically changes the It tries to not replace letters that are inside HTML tags or special HTML characters, but if you have complicated or invalid HTML markup in your
|
|
Function |
no | n/a |
The first argument to the function is the text that the user has entered, the second argument are the Option Objects matched by the default algorithm, the third argument is an array of all Option Objects for the current list, and the fourth argument is the current value of the AutoComplete widget. The function should return an array of Option Objects. You can return an array of any Option Objects from this function. They do not have to come from the existing Option Objects in the list. This function gets executed with every 'keydown' event on the input element so it's in your best interest to make this function as fast as possible. |
|
String or |
no | 'No results found.' |
An HTML string to display when there are no options to show. If The function should return an HTML string.
|
|
String or |
no | n/a |
If If The function should return an HTML string. If there is an |
|
String or |
no | n/a |
If If The function should return an HTML string. If there is a |
|
Array of Option Objects |
no | [] |
|
Option Objects are the meat of the AutoComplete widget. They are the options displayed to the user as they type.
Option Objects can hold any arbitrary value; they are not limited to what the user sees on the screen.
An Option Object becomes a Token Object when the user selects it from the dropdown list.
You can define the list workflow using the children
property on Option Objects.
As a shorthand method, you can use a string as an Option Object. See the allowFreeform example.
Property / Type | Required | Default | Description | Example |
---|---|---|---|---|
String or |
no | n/a |
When any option from the list is selected from the dropdown, If This |
|
String |
no | n/a |
An HTML string to show inside the dropdown. This If there is no |
|
String |
no | n/a |
An HTML string to show inside the search bar. This If there is no |
|
String or |
yes | n/a |
|
Token Objects are what is stored on the search bar when a user selects an option from the dropdown menu.
A Token Group is an array of Token Objects.
The value of the search bar is an array of Token Groups.
Token Objects are not explicitly defined in the AutoComplete config; they are created from an Option Object using the option.value
and option.tokenHTML
properties.
You can use a string as shorthand for a Token Object when using the setValue
method.
Property / Type | Required | Default | Description | Example |
---|---|---|---|---|
String |
yes | n/a |
It comes from the |
|
String or |
yes | n/a |
It comes from the |
Each AutoComplete object has methods you can use to interact with the widget.
Method | Args | Description | Example |
---|---|---|---|
addOption(name, option) |
|
Add an option to a List Object. Returns Returns |
|
blur() |
none |
Remove focus from the widget. |
|
clear() |
none |
Clear the value of the widget. Has the same effect as calling |
|
destroy() |
none |
Remove the widget from the DOM. |
|
focus() |
none |
Puts the input focus on the widget. |
|
getList(name) |
|
Returns the List Object if it exists. Returns |
|
getLists() |
none |
Returns an object of all the List Objects. |
|
getValue() |
none |
Returns the current value of the widget. Returns an empty array if the widget has no Token Groups. |
|
list(name) |
|
Alias of |
|
list(name, list) |
|
Alias of |
|
pressDown() |
none |
Simulates the user pressing the "Down Arrow" key on the input field. Has no effect if the input field does not have focus. |
|
pressEnter() |
none |
Simulates the user pressing the "Enter" key on the input field. Has no effect if the input field does not have focus. |
|
pressUp() |
none |
Simulates the user pressing the "Up Arrow" key on the input field. Has no effect if the input field does not have focus. |
|
removeList(name) |
|
Returns Returns You cannot remove the |
|
removeTokenGroup(index) |
|
Remove a Token Group by array index. Returns the updated value of the widget if successful. Returns |
|
setInput(input) |
|
Puts text into the input element as if the user had typed it. Returns Returns |
|
setList(name, list) |
|
Adds a new list or updates an existing list. Returns Returns |
|
setValue(value) |
|
Returns Returns |
|
val() |
none |
Alias of |
|
val(value) |
|
Alias of |
AutoComplete has an error system designed to inform you when you use the API incorrectly.
Every alert has a unique code associated with it and you can control how the errors are presented with the showErrors config option.
Error ID | Error Text | More Information |
---|---|---|
1002 | Element with id "<id>" does not exist in the DOM. |
AutoComplete could not find your element with Please note that if you pass a string as the first argument to the AutoComplete() constructor it should be the value of a DOM id, not a CSS selector (ie: |
1003 | JSON does not exist. Please include a JSON polyfill. |
AutoComplete requires a JSON implementation. Please include a polyfill for older browsers. |
1004 | Unable to find a valid version of jQuery. Please include jQuery 1.4.2 or higher on the page. |
AutoComplete requires jQuery version 1.4.2 or higher. |
1005 | You must include at least one List Object on lists. |
The |
1037 | The first argument to AutoComplete() cannot be an empty string. |
The first argument to the AutoComplete() constructor should be the id of a DOM element or a reference to a single DOM element. |
1044 | The first argument to AutoComplete() must be an ID or a single DOM node. |
The first argument to the AutoComplete() constructor should be the id of a DOM element or a reference to a single DOM element. |
1273 | You cannot include properties that effect control flow on ajaxOpts: "async", "complete", "error", "statusCode", "success" |
You are not allowed to override certain AJAX properties. See ajaxOpts. |
1328 | Error in removeList method. List "<listName>" does not exist. |
The list name you provided to the |
1424 | Error in removeList method. You cannot remove the initialList "<listName>" |
You are not allowed to remove the |
2231 | The first argument to the removeList method must be a non-empty string. |
The first argument to |
2535 | The list object for list "<listName>" is invalid. |
The structure of your List Object is invalid. |
2642 | You cannot use an empty string for a list name. |
It is valid to use an empty string for an object property name in JavaScript, but this is not allowed by AutoComplete. |
2728 | initialList "<listName>" does not exist on the lists object. |
The list you specified on |
2732 | The list object passed to the setList method is not valid. |
The structure of your List Object is invalid. |
2789 | The first argument to the getList method must be a non-empty string. |
The first argument to |
3193 | Could not find a valid value for optionHTML. Resorted to using value property "<propertyName>" |
AutoComplete could not find a valid |
3776 | Invalid Value returned from your custom onChange function. |
If you specify an |
3843 | optionHTML function did not return a string. |
If you provide a function for |
4823 | Error in removeTokenGroup method. Token group index "<tokenGroupIndex>" does not exist. |
You tried to remove a Token Group array index which doesn't exist. |
4922 | The first argument to the setInput method must be a string. |
The argument passed to |
5732 | Error in addOption method. List "<listName>" does not exist. |
The list name you provided to the |
5783 | Unable to create HTML string for optionHTML. |
This is an internal AutoComplete error that you should never see. If you see this error please open a GitHub issue. |
5938 | Wrong number of arguments passed to list method. |
The |
6447 | Invalid value pass to initialValue. |
The value passed to |
6823 | Invalid value passed to the setValue method. |
The value passed to the |
7283 | The first argument to the setList method must be a non-empty string. |
The first argument to |
7887 | Invalid option passed to addOption method. |
You must pass a valid Option Object to the |
7998 | Your tokenSeparatorHTML function did not return a string. |
If you provide a function to tokenSeparatorHTML, it must return a string. |
8292 | Could not find optionID "<optionId>". |
This is an internal AutoComplete error that you should never see. If you see this error please open a GitHub issue. |
8366 | The first argument to the addOption method must be a non-empty string. |
The list name you provided to the |
8721 | AJAX url must be a string. Did you forget to include one on ajaxOpts? |
If you provide a custom function on |
9992 | Wrong number of arguments passed to val method. |
The |