jQuery UI Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.
jQuery UI Autocomplete can be applied on any input field, in other words any HTML field of type <input> or on a textarea, in other words <textarea>.
How does jQuery UI Autocomplete help?
It helps to do the intellisense or to display the available options to the user for a particular input field. Basically it acts as a plugin when integrated with any TextBox. Start searching for the matching records (filtering the records from the available or the provided source) and display it to the user for the selection. The user can filter the records for better matches by entering more precise values. It also helps in achieving some functionality like, on entering a specific zip code something is automatically filled in. The data source for the autocomplete can be local (present on the client side) or it can be remote (present on the server for example database).
Autocomplete also provides the user interaction with the list via the keyboard also. So basically the user can move up and down using arrow keys.
Applying styles on the Autocomplete box and the selection list
One can also customize the look and feel of the autocomplete TextBox and the selection list. By default the jQuery UI CSS styles are used and one can modify them by extending the CSS classes (by extending I mean overwriting the styles in the applied classes).
Various functionalities and properties (some of the frequently used properties) exposed by jQuery UI Autocomplete are:
- Autofocus: If this property is set to "true" then the first item in the selection list is automatically selected when the options are shown.
- Delay: One can set the delay required, this allows configuration of how much time the available options are shown in the selection list or the drop down that is opened below the input field. So if the input is local then the filtration on the local data source is performed after the specified delay and then the filtered records are displayed. And if the data source is a remote data source then the server is made to get the filtered records from the database after the specified delay. It helps in preventing the frequent server hits to the server or minimizes the filtration processing if the data source is local. What actually happens is that if the delay is 1000 ms and the user continues to enter the values (characters) in the TextBox then the server hit occurs after the 1000 ms when the user stops entering the values.
- Minlength: This property specifies the minimum required characters that the user should input before looking for the available optionvalues. So if it is set to 3 then the filtered records (if any are present matching those characters depending upon your filtration criteria) are shown only when the user enters three characters.
- Position: Position of the selection list can be specified relative to the input field. Some of the available options are LeftTop, LeftBottom, RightTop, RightBottom. The position of the selection list can also be specified relative to the other elements present on the same mask.
- Source: This is the actual data source of the options available to the user. It can be Local or Remote. To specify the local data source, one must provide an array of items (items can be just a string or can be an object with a label and value properties, label if the property for the value that will be shown in the selection list or the drop down list whereas value is the property for the value or the string that will be shown inside the input field when the user selects an option. If one of them is specified then it applies to both properties). To specify the remote data source one has to give a URL string (that returns the JSON data) to the source. The server can get the value the user entered (on which the filtration can be performed on the server dynamically) in a query string parameter named "term" when an AJAX get request is made.
Various Methods exposed by jQuery UI Autocomplete
Some methods available help in controlling the selection list via script (in other words JavaScript or jQuery scripting). I will not go into too much details of it since they are self-explanatory (by their names) and not used very frequently since one rarely needs to play around with the Autocomplete box via script.
- Close: This method closes the auto complete selection list
- Disable: This method disables the autocomplete feature on an input field
- Enable: If you disable the autocomplete feature then there must be some way to enable it, this is the method for doing that.
- Option (3 Overloads): One to get a specified option (by label; I hope you can now understand the difference between label and value in autocomplete), one to set the value of specified option, and one to get the entire option hash (in other words the data source or one can say the filtered data source if the user tried to filter it depending upon some input in the input box)
- Search: You can forcefully make a search (maybe on a click of some button present in your form) on a particular value or the value in the input field entered by the user using the "this" method.
Various Events exposed or triggered by jQuery UI Autocomplete
These events will help one in achieving custom functionalities or to do something on the back end when the user starts playing (or using) the control.
- Change Event: As the name indicates and for the developers familiar with the JavaScript onchange event, this is triggered if the focus is moved out and the value gets changed in the input field.
- Close Event: This event is fired when the selection list or the drop down list showing the available option to the user is closed.
- Create Event: This event is fired on the creation of autocomplete much more like an init event. For those who are familiar with the init event they can easily use it but for those unfamiliar with it, this is the event that every control exposes (independent of the technology) and is fired when the control is created.
- Focus Event: This event is fired when a particular option in the selection list is focussed on by the user by using keyboard keys (Arrow keys up or down). Don't confuse it with the mouse hover. It is fired for keyboard interaction, is only fired when the user is focussing a particular option by moving into the available option list up and down. It is not fired on selection. The default functionality attached to this event is to place the focussed option value into the input field or the text field.
- Open Event: This event is fired whenever the new search is performed and the selection list is opened with the filtered items. So this event is also fired when the search is performed and there are no suggested options that can be presented to the user. Consider that this event is fired in case of an update of the filtered options (presented to the user).
- Response Event: This event is fired when the search is performed before the available options are shown to the user. So if one wants to manipulate the result set or write down some custom login to modify the result or to do anything before the result is shown to the user, this event is the best place.
- Search Event: Once the user enters the minimum characters required to make a search then the search request is made to get the suggestion data (In case of a delay, the specified search is made after a delay interval from the mentioned scenario). So just before making the request to get the suggestion data Search event is fired. If one needs to do anything custom before making a request to get the suggestion data, this event is helpful.
- Select Event: On this event the selected item from the selection list is populated to the input field or the text field and the selection list is closed. This is the default behaviour for this event. By user selection I mean that a user is traversing down the available options by keyboard keys and then presses enter to select a particular item or user selects an item from the list via mouse click.
This is most of the details of the jQuery UI Autocomplete; I think we can now go into the technical details of how we can implement it. I am a guy from the .Net background (using C#), so I am sorry but my implementation examples somehow will be in the same technology or language (especially the server side code showing the remote search). But it doesn't mean that it will not work in Java Web forms (JSP) or may be some other pages. Here I will be presenting how it can be implemented in simple HTML pages.
Some of the prerequisites for using jQuery UI Autocomplete are:
- jQuery Library: Can be downloaded from CDN (Content Delivery Network: http://code.jquery.com/jquery-1.9.1.js) or one can give link directly.
- jQuery UI Library: Can be downloaded from CDN (Content Delivery Network: http://code.jquery.com/ui/1.10.3/jquery-ui.js) or one can give link directly.
- jQuery UI CSS: Can be downloaded from CDN (Content Delivery Network: http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css) or one can give link directly.
One can also use the minified version of all of these or any other version (Just ensure that they are using the required version of the dependent library).
Example of Local Data Source
The HTML page complies with HTML5 standards.
HTML
<!doctype html>
<html>
<head>
<title>Local Data Source Sample</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" language="javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"
language="javascript"></script>
</head>
<body>
<input id="txtTest" type="text" />
</body>
<script type="text/javascript"
language="javascript">
$("#txtTest").autocomplete({
source: ["Test Value 1", "Test Value 2", "Test Value 3", "Unique Value"]
});
</script>
</html>
Screenshot of the running sample:
This is a very simple example with the static data source.
Example of Remote Data Source
Here I will show you the use of some of the properties and events of the Autocomplete. We will make an AJAX request on the server to get the data during runtime in JSON format. Values from the client will be passed in the form of a query string and a HTTP Handler will be created to serve the request.
HTML
<!doctype html>
<html>
<head>
<title>Remote Data Source Sample</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" language="javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"
language="javascript"></script>
</head>
<body>
<input id="txtTest" type="text" />
</body>
<script type="text/javascript" language="javascript">
$("#txtTest").autocomplete({
source: function (request, response) {
$.ajax({
url: "/default.Test", //Request is being made to the handler for extension test
dataType: "json",
data: {
term: request.term//You can also send more query string parameters here seperated by comma. request.term will send the value present in the textbox
},
success: function (data) {
if (data != null && data != '') {
response($.map(data, function (item) {
return {
label: item.FirstName + ' ' + item.LastName,
value: item.FirstName + ' ' + item.LastName //You can add more properties here
};
}));
} else {
$('ul[class*=ui-autocomplete]').hide();
}
}
});
},
minLength: 3,
cache: false,
select: function (event, ui) {
if (ui != null && ui.item != null) {
$("#txtTest").val(ui.item.value);
}
//The below code is written to prevent the execution of default button functionality on press of ENTER key
var code = (event.keyCode ? event.keyCode : event.which);
if (code == 13) {
event.preventDefault();
event.stopPropagation();
}




Dinesh BeniwalPosted Aug 7, 2013, 1:05 AM
Good Start Abhishek, Welcome to the C# Corner.