Introduction
Autocomplete is one of the best widgets in websites and is used in nearly all websites. jQuery has a powerful widget, autocomplete, and in this article I will try to explain how to use jQuery autocomplete in websites. All the way and all other features of autocomplete. We can make autocomplete, using AJAX, to call to build a list (server-side) and then bind that list into a text box using JavaScript. However there are other alternative to make autocomplete rather then this in an easy way.
The most robust and efficient tool of autocomplete is jQuery-ui autocomplete and this tool is free and there is no need to license it. Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, this tool reduces the overhead of searching and filtering.
Any field that can receive input can be converted into an Autocomplete, like <input> element, <textarea> element and any other element having the contenteditable attribute.
Points to remember
- The Autocomplete widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS as a starting point.
- This widget manipulates its element's value programmatically, therefore a native change event may not be fired when the element's value changes.
List of examples
- Default functionality
- Accent folding
- Cumbobox
- Multiple values
- Remote Datasource
- Scrollable result
Default functionality
In this category, we will see the code, output screen and syntax.
JavaScript code
- $(document).ready(function () {
- var a=[
- "one",
- "two",
- "three",
- “four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "ten",
- "eleven",
- "tweleve"
- "thirteen",
- "fourteen", ];
- $("#auto").autocomplete({
- source: a
- });
- });
In this code we have created a data source, the data source is in array and that source is bound with the <input> id. element.
HTML code
- <html >
- <head>
- <title>jQuery UI Autocomplete - Default functionality</title>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
- <script src="Scripts/jquery-ui-1.10.4.min.js"></script>
- <script src="Scripts/jquery-1.6.4.js"></script>
- <script src="Scripts/jquery-ui-1.10.4.min.js"></script>
- <script src="Scripts/AutoComplete.js"></script>
- </head>
- <body>
- <input type="text" id="auto" />
- </body>
Accent folding
In this option the autocomplete field uses a custom source option that matches the results on the basis of accented characters, even when the text field doesn't contain an accented character. If we type an accented character in the text field it is smart enough not to show the result that are not accented.
JavaScript code
- $(document).ready(function () {
- var names = ["rájeev", "ranjan", "röhan", "rashmi"];
- var accentMap = {
- "á": "a",
- "ö": "o"
- };
- var normalize = function (term) {
- var ret = "";
- for (var i = 0; i < term.length; i++) {
- ret += accentMap[term.charAt(i)] || term.charAt(i);
- }
- return ret;
- };
-
- $("#name").autocomplete({
- source: function (request, response) {
- var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
- response($.grep(names, function (value) {
- value = value.label || value.value || value;
- return matcher.test(value) || matcher.test(normalize(value));
- }));
- }
- });
- });
HTML code
- <body>
- <label for="name">Name: </label>
- <input id="name">
- </body>
Combobox
Cumbobox is a custom widget, that is composition of Autocomplete and button. we can either type something into the field to get filtered suggestion based on our input, or use the button to get the full list of selections.The input is read from existing select-element for progressive enhancement, passed to Autocomplete with a customized source- option.
Multiple values
This is also a custom widget of autocomplete, in this we can search by any character and display multiple values into a single field.
Remote datasource
The autocomplete widget provides suggestions while we type into field, and the datasource is a server-side side data , either JASON data, any any web services.
Scrollable result
When we type any character in text box the filtered result are shows in scrollable format.
- <html>
- <head>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
- <script src="Scripts/jquery-ui-1.10.4.min.js"></script>
- <script src="Scripts/jquery-1.6.4.js"></script>
- <script src="Scripts/jquery-ui-1.10.4.min.js"></script>
- <title></title>
- <style>
- .ui-autocomplete {
- max-height: 100px;
- overflow-y: auto;
- /* prevent horizontal scrollbar */
- overflow-x: hidden;
- }
- * html .ui-autocomplete {
- height: 100px;
- }
- </style>
- <script>
- $(function () {
- var availableTags = [
- "ActionScript",
- "AppleScript",
- "Asp",
- "BASIC",
- "C",
- "C++",
- "Clojure",
- "COBOL",
- "ColdFusion",
- "Erlang",
- "Fortran",
- "Groovy",
- "Haskell","
- Java",
- "JavaScript",
- "Lisp",
- "Perl",
- "PHP",
- "Python",
- "Ruby",
- "Scala",
- "Scheme
- ];
- $("#tags").autocomplete({
- source: availableTags
- });
- });
- </script>
- </head>
- <body>
- <div class="ui-widget">
- <label for="tags">Tags: </label>
- <input id="tags">
- </div>
- </body>
- </html>
Summary
In this article we saw autocomplete and various ways of using the jQuery autocomplete widget.