Algolia Autocomplete.js

Fast and full-featured autocomplete library

README

⚠️ Deprecation notice

>

Autocomplete.js (v0.x) is deprecated and no longer supported. Please use Autocomplete instead.

To upgrade from v0.x, please follow our upgrade guide.


Autocomplete.js



This JavaScript library adds a fast and fully-featured auto-completion menu to your search box displaying results "as you type". It can easily be combined with Algolia's realtime search engine. The library is available as a jQuery plugin, an Angular.js directive or a standalone library.
build status NPM version Coverage Status jsDelivr Hits
jQuery
Zepto.js
Angular.js License: MIT

Browser tests

Table of Contents






  - jsDelivr
  - cdnjs
  - npm
  - Bower
  - jQuery
  - Hits
- FAQ
  - [How can I Control-click on results and have them open in a new tab?](#how-can-i-control-click-on-results-and-have-them-open-in-a-new-tab)
- API
  - jQuery



Features


Displays suggestions to end-users as they type
Shows top suggestion as a hint (i.e. background text)
Supports custom templates to allow for UI flexibility
Works well with RTL languages and input method editors
Triggers custom events


Installation


The autocomplete.js library must be included after jQuery, Zepto or Angular.js (with jQuery). Else, it will use the embedded Zepto.

jsDelivr


  1. ``` html
  2. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
  3. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.jquery.min.js"></script>
  4. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.angular.min.js"></script>
  5. ```

cdnjs


  1. ``` html
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/<VERSION>/autocomplete.min.js"</script>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/<VERSION>/autocomplete.jquery.min.js"</script>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/<VERSION>/autocomplete.angular.min.js"</script>
  5. ```

npm


  1. ```sh
  2. npm install --save autocomplete.js
  3. ```

Bower


  1. ```sh
  2. bower install algolia-autocomplete.js -S
  3. ```

Source dist/


You can find the built version in dist/.

Browserify


You can require it and use Browserify:

  1. ``` js
  2. var autocomplete = require('autocomplete.js');
  3. ```

Usage


Standalone


1. Include autocomplete.min.js
1. Initialize the auto-completion menu calling the autocomplete function

Warning: autocomplete.js is not compatible with the latest version algoliasearch v4 out of the box, but you can create a compatibility source by yourself like this:

  1. ``` html
  2. <input type="text" id="search-input" placeholder="Search unicorns..." />
  3. <script src="https://cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch.umd.min.js"></script>
  4. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
  5. <script>
  6.   var client = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey');
  7.   var index = client.initIndex('YourIndex');
  8.   function newHitsSource(index, params) {
  9.     return function doSearch(query, cb) {
  10.       index
  11.         .search(query, params)
  12.         .then(function(res) {
  13.           cb(res.hits, res);
  14.         })
  15.         .catch(function(err) {
  16.           console.error(err);
  17.           cb([]);
  18.         });
  19.     };
  20.   }
  21.   autocomplete('#search-input', { hint: false }, [
  22.     {
  23.       source: newHitsSource(index, { hitsPerPage: 5 }),
  24.       displayKey: 'my_attribute',
  25.       templates: {
  26.         suggestion: function(suggestion) {
  27.           return suggestion._highlightResult.my_attribute.value;
  28.         }
  29.       }
  30.     }
  31.   ]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
  32.     console.log(event, suggestion, dataset, context);
  33.   });
  34. </script>
  35. ```

jQuery


1. Include autocomplete.jquery.min.js after including jQuery
1. Initialize the auto-completion menu calling the autocomplete jQuery plugin
Warning: autocomplete.js is not compatible with the latest version algoliasearch v4, therefore we highly recommend you use algoliasearch v3 as specified in the code snippet below.

  1. ``` html
  2. <input type="text" id="search-input" />
  3. <script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
  4. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.jquery.min.js"></script>
  5. <script>
  6.   var client = algoliasearch('YourApplicationID', 'YourSearchOnlyAPIKey')
  7.   var index = client.initIndex('YourIndex');
  8.   $('#search-input').autocomplete({ hint: false }, [
  9.     {
  10.       source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5 }),
  11.       displayKey: 'my_attribute',
  12.       templates: {
  13.         suggestion: function(suggestion) {
  14.           return suggestion._highlightResult.my_attribute.value;
  15.         }
  16.       }
  17.     }
  18.   ]).on('autocomplete:selected', function(event, suggestion, dataset, context) {
  19.     console.log(event, suggestion, dataset, context);
  20.   });
  21. </script>
  22. ```

Angular.JS


1. Include autocomplete.angular.min.js after including jQuery & Angular.js
1. Inject the algolia.autocomplete module
1. Add the autocomplete, aa-datasets and the optional aa-options attribute to your search bar

Warning: autocomplete.js is not compatible with the latest version algoliasearch v4, therefore we highly recommend you use algoliasearch v3 as specified in the code snippet below.

  1. ``` html
  2. <div ng-controller="yourController">
  3.   <input type="text" id="search-input" autocomplete aa-datasets="getDatasets()" />
  4. </div>
  5. <script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.angular.min.js"></script>
  6. <script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.angular.min.js"></script>
  7. <script>
  8.   angular.module('myApp', ['algoliasearch', 'algolia.autocomplete'])
  9.     .controller('yourController', ['$scope', 'algolia', function($scope, algolia) {
  10.       var client = algolia.Client('YourApplicationID', 'YourSearchOnlyAPIKey');
  11.       var index = client.initIndex('YourIndex');
  12.       $scope.getDatasets = function() {
  13.         return {
  14.           source: algolia.sources.hits(index, { hitsPerPage: 5 }),
  15.           displayKey: 'my_attribute',
  16.           templates: {
  17.             suggestion: function(suggestion) {
  18.               return suggestion._highlightResult.my_attribute.value;
  19.             }
  20.           }
  21.         };
  22.       };
  23.       $scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
  24.         console.log(event, suggestion, dataset, context);
  25.       });
  26.     }]);
  27. </script>
  28. ```

Note: You need to rely on jQuery, the lite version embedded in Angular.js won't work.

Look and Feel


Below is a faux mustache template describing the DOM structure of an autocomplete
dropdown menu. Keep in mind that header, footer, suggestion, and empty
come from the provided templates detailed here.

  1. ``` html
  2. <span class="aa-dropdown-menu{{#datasets}} aa-{{'with' or 'without'}}-{{name}}{{/datasets}}">
  3.   {{#datasets}}
  4.     <div class="aa-dataset-{{name}}">
  5.       {{{header}}}
  6.       <span class="aa-suggestions">
  7.         {{#suggestions}}
  8.           <div class="aa-suggestion">{{{suggestion}}}</div>
  9.         {{/suggestions}}
  10.         {{^suggestions}}
  11.           {{{empty}}}
  12.         {{/suggestions}}
  13.       </span>
  14.       {{{footer}}}
  15.     </div>
  16.   {{/datasets}}
  17.   {{empty}}
  18. </span>
  19. ```

When an end-user mouses or keys over a .aa-suggestion, the class aa-cursor will be added to it. You can use this class as a hook for styling the "under cursor" state of suggestions.


Add the following CSS rules to add a default style:

  1. ```css
  2. .algolia-autocomplete {
  3.   width: 100%;
  4. }
  5. .algolia-autocomplete .aa-input, .algolia-autocomplete .aa-hint {
  6.   width: 100%;
  7. }
  8. .algolia-autocomplete .aa-hint {
  9.   color: #999;
  10. }
  11. .algolia-autocomplete .aa-dropdown-menu {
  12.   width: 100%;
  13.   background-color: #fff;
  14.   border: 1px solid #999;
  15.   border-top: none;
  16. }
  17. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion {
  18.   cursor: pointer;
  19.   padding: 5px 4px;
  20. }
  21. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor {
  22.   background-color: #B2D7FF;
  23. }
  24. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion em {
  25.   font-weight: bold;
  26.   font-style: normal;
  27. }
  28. ```

Here is what the basic example looks like:

Basic example

Global Options


When initializing an autocomplete, there are a number of global options you can configure.

* `autoselect` – If `true`, the first rendered suggestion in the dropdown will automatically have the `cursor` class, and pressing `` will select it.

* `autoselectOnBlur` – If `true`, when the input is blurred, the first rendered suggestion in the dropdown will automatically have the `cursor` class, and pressing `` will select it. This option should be used on mobile, see [#113](https://github.com/algolia/autocomplete.js/issues/113)

tabAutocomplete – If true, pressing tab will select the first rendered suggestion in the dropdown. Defaults to true.

hint – If false, the autocomplete will not show a hint. Defaults to true.

debug – If true, the autocomplete will not close on blur. Defaults to false.

clearOnSelected – If true, the autocomplete will empty the search box when a suggestion is selected. This is useful if you want to use this as a way to input tags using the selected event.

openOnFocus – If true, the dropdown menu will open when the input is focused. Defaults to false.

appendTo – If set with a DOM selector, doesn't wrap the input and appends the wrapper and dropdown menu to the first DOM element matching the selector. It automatically positions the wrapper under the input, and sets it to the same width as the input. Can't be used with hint: true, because hint requires the wrapper around the input.

dropdownMenuContainer – If set with a DOM selector, it overrides the container of the dropdown menu.

templates – An optional hash overriding the default templates.
  dropdownMenu – the dropdown menu template. The template should include all dataset placeholders.
  header – the header to prepend to the dropdown menu
  footer – the footer to append to the dropdown menu
  empty – the template to display when none of the datasets are returning results. The templating function
    is called with a context containing the underlying query.

cssClasses – An optional hash overriding the default css classes.
  root – the root classes. Defaults to algolia-autocomplete.
  prefix – the CSS class prefix of all nested elements. Defaults to aa.
  noPrefix - set this to true if you wish to not use any prefix. Without this option, all nested elements classes will have a leading dash. Defaults to false.
  dropdownMenu – the dropdown menu CSS class. Defaults to dropdown-menu.
  input – the input CSS class. Defaults to input.
  hint – the hint CSS class. Defaults to hint.
  suggestions – the suggestions list CSS class. Defaults to suggestions.
  suggestion – the suggestion wrapper CSS class. Defaults to suggestion.
  cursor – the cursor CSS class. Defaults to cursor.
  dataset – the dataset CSS class. Defaults to dataset.
  empty – the empty CSS class. Defaults to empty.

keyboardShortcuts - Array of shortcut that will focus the input. For example if you want to bind s and /
you can specify: keyboardShortcuts: ['s', '/']

ariaLabel - An optional string that will populate the aria-label attribute.

  1. ``` html
  2. <script type="text/template" id="my-custom-menu-template">
  3.   <div class="my-custom-menu">
  4.     <div class="row">
  5.       <div class="col-sm-6">
  6.         <div class="aa-dataset-d1"></div>
  7.       </div>
  8.       <div class="col-sm-6">
  9.         <div class="aa-dataset-d2"></div>
  10.         <div class="aa-dataset-d3"></div>
  11.       </div>
  12.     </div>
  13.   </div>
  14. </script>
  15. <style>
  16. body {
  17. font-family: -apple-system, sans-serif;
  18. }
  19. .algolia-autocomplete {
  20.   width: 100%;
  21. }
  22. .algolia-autocomplete .aa-input, .algolia-autocomplete .aa-hint {
  23.   width: 100%;
  24. }
  25. .algolia-autocomplete .aa-hint {
  26.   color: #999;
  27. }
  28. .algolia-autocomplete .aa-dropdown-menu {
  29.   width: 100%;
  30.   background-color: #fff;
  31.   border: 1px solid #999;
  32.   border-top: none;
  33. }
  34. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion {
  35.   cursor: pointer;
  36.   padding: 5px 4px;
  37. }
  38. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion.aa-cursor {
  39.   background-color: #B2D7FF;
  40. }
  41. .algolia-autocomplete .aa-dropdown-menu .aa-suggestion em {
  42.   font-weight: bold;
  43.   font-style: normal;
  44. }
  45. .branding {
  46. font-size: 1.3em;
  47. margin: 0.5em 0.2em;
  48. }
  49. .branding img {
  50. height: 1.3em;
  51. margin-bottom: - 0.3em;
  52. }
  53. </style>
  54. <script>
  55.   $('#search-input').autocomplete(
  56.     {
  57.       templates: {
  58.         dropdownMenu: '#my-custom-menu-template',
  59.         footer: '<div class="branding">Powered by <img src="https://www.algolia.com/static_assets/images/press/downloads/algolia-logo-light.svg" /></div>'
  60.       }
  61.     },
  62.     [
  63.       {
  64.         source: $.fn.autocomplete.sources.hits(index1, { hitsPerPage: 5 }),
  65.         name: 'd1',
  66.         templates: {
  67.           header: '<h4>List 1</h4>',
  68.           suggestion: function(suggestion) {
  69.             // FIXME
  70.           }
  71.         }
  72.       },
  73.       {
  74.         source: $.fn.autocomplete.sources.hits(index2, { hitsPerPage: 2 }),
  75.         name: 'd2',
  76.         templates: {
  77.           header: '<h4>List 2</h4>',
  78.           suggestion: function(suggestion) {
  79.             // FIXME
  80.           }
  81.         }
  82.       },
  83.       {
  84.         source: $.fn.autocomplete.sources.hits(index3, { hitsPerPage: 2 }),
  85.         name: 'd3',
  86.         templates: {
  87.           header: '<h4>List 3</h4>',
  88.           suggestion: function(suggestion, answer) {
  89.             // FIXME
  90.           }
  91.         }
  92.       }
  93.     ]
  94.   );
  95. </script>
  96. ```

minLength – The minimum character length needed before suggestions start
  getting rendered. Defaults to 1.

autoWidth – This option allow you to control the width of autocomplete wrapper. When false the autocomplete wrapper will not have the width style attribute and you are be able to put your specific width property in your css to control the wrapper. Default value is true.

One scenario for use this option. e.g. You have a max-width css attribute in your autocomplete-dropdown-menu and you need to width grows until fill the max-width. In this scenario you put a width: auto in your autocomplete wrapper css class and the max-width in your autocomplete dropdown class and all done.

Datasets


An autocomplete is composed of one or more datasets. When an end-user modifies the
value of the underlying input, each dataset will attempt to render suggestions for the
new value.

Datasets can be configured using the following options.

source – The backing data source for suggestions. Expected to be a function
  with the signature (query, cb). It is expected that the function will
  compute the suggestion set (i.e. an array of JavaScript objects) for query
  and then invoke cb with said set. cb can be invoked synchronously or
  asynchronously.

name – The name of the dataset. This will be appended to tt-dataset- to
  form the class name of the containing DOM element.  Must only consist of
  underscores, dashes, letters (a-z), and numbers. Defaults to a random
  number.

displayKey – For a given suggestion object, determines the string
  representation of it. This will be used when setting the value of the input
  control after a suggestion is selected. Can be either a key string or a
  function that transforms a suggestion object into a string. Defaults to
  value.
  Example function usage: displayKey: function(suggestion) { return suggestion.nickname || suggestion.firstName }

templates – A hash of templates to be used when rendering the dataset. Note
  a precompiled template is a function that takes a JavaScript object as its
  first argument and returns a HTML string.

  empty – Rendered when 0 suggestions are available for the given query.
  Can be either a HTML string or a precompiled template. The templating function
  is called with a context containing query, isEmpty, and any optional
  arguments that may have been forwarded by the source:
  function emptyTemplate({ query, isEmpty }, [forwarded args]).

  footer – Rendered at the bottom of the dataset. Can be either a HTML
  string or a precompiled template. The templating function
  is called with a context containing query, isEmpty, and any optional
  arguments that may have been forwarded by the source:
  function footerTemplate({ query, isEmpty }, [forwarded args]).

  header – Rendered at the top of the dataset. Can be either a HTML string
  or a precompiled template. The templating function
  is called with a context containing query, isEmpty, and any optional
  arguments that may have been forwarded by the source:
  function headerTemplate({ query, isEmpty }, [forwarded args]).

  suggestion – Used to render a single suggestion. The templating function
  is called with the suggestion, and any optional arguments that may have
  been forwarded by the source: function suggestionTemplate(suggestion, [forwarded args]).
Defaults to the value of `displayKey` wrapped in a `p` tag i.e. `

{{value}}

`.

debounce – If set, will postpone the source execution until after debounce milliseconds
have elapsed since the last time it was invoked.

cache - If set to false, subsequent identical queries will always execute the source function for suggestions. Defaults to true.

Sources


A few helpers are provided by default to ease the creation of Algolia-based sources.

Hits


To build a source based on Algolia's hits array, just use:

  1. ``` js
  2. {
  3.   source: autocomplete.sources.hits(indexObj, { hitsPerPage: 2 }),
  4.   templates: {
  5.     suggestion: function(suggestion, answer) {
  6.       // FIXME
  7.     }
  8.   }
  9. }
  10. ```

PopularIn (aka "xxxxx in yyyyy")


To build an Amazon-like autocomplete menu, suggesting popular queries and for the most popular one displaying the associated categories, you can use the popularIn source:

  1. ``` js
  2. {
  3.   source: autocomplete.sources.popularIn(popularQueriesIndexObj, { hitsPerPage: 3 }, {
  4.     source: 'sourceAttribute',           // attribute of the `popularQueries` index use to query the `index` index
  5.     index: productsIndexObj,             // targeted index
  6.     facets: 'facetedCategoryAttribute',  // facet used to enrich the most popular query
  7.     maxValuesPerFacet: 3                 // maximum number of facets returned
  8.   }, {
  9.     includeAll: true,                    // should it include an extra "All department" suggestion
  10.     allTitle: 'All departments'          // the included category label
  11.   }),
  12.   templates: {
  13.     suggestion: function(suggestion, answer) {
  14.       var value = suggestion.sourceAttribute;
  15.       if (suggestion.facet) {
  16.         // this is the first suggestion
  17.         // and it has been enriched with the matching facet
  18.         value += ' in ' + suggestion.facet.value + ' (' + suggestion.facet.count + ')';
  19.       }
  20.       return value;
  21.     }
  22.   }
  23. }
  24. ```

Custom source


The source options can also take a function. It enables you to have more control of the results returned by Algolia search. The function function(query, callback) takes 2 parameters
  query: String: the text typed in the autocomplete
  callback: Function: the callback to call at the end of your processing with the array of suggestions

  1. ``` js
  2. source: function(query, callback) {
  3.   var index = client.initIndex('myindex');
  4.   index.search(query, { hitsPerPage: 1, facetFilters: 'category:mycat' }).then(function(answer) {
  5.     callback(answer.hits);
  6.   }, function() {
  7.     callback([]);
  8.   });
  9. }
  10. ```

Or by reusing an existing source:

  1. ``` js
  2. var hitsSource = autocomplete.sources.hits(index, { hitsPerPage: 5 });

  3. source: function(query, callback) {
  4.   hitsSource(query, function(suggestions) {
  5.     // FIXME: Do stuff with the array of returned suggestions
  6.     callback(suggestions);
  7.   });
  8. }
  9. ```

Security


User-generated data: protecting against XSS


Malicious users may attempt to engineer XSS attacks by storing HTML/JS in their data. It is important that user-generated data be properly escaped before using it in an autocomplete.js template.

In order to easily do that, autocomplete.js provides you with a helper function escaping all HTML code but the highlighting tags:

  1. ``` js
  2.   templates: {
  3.     suggestion: function(suggestion) {
  4.       var val = suggestion._highlightResult.name.value;
  5.       return autocomplete.escapeHighlightedString(val);
  6.     }
  7.   }
  8. ```

If you did specify custom highlighting pre/post tags, you can specify them as 2nd and 3rd parameter:

  1. ``` js
  2.   templates: {
  3.     suggestion: function(suggestion) {
  4.       var val = suggestion._highlightResult.name.value;
  5.       return autocomplete.escapeHighlightedString(val, '<span class="highlighted">', '</span>');
  6.     }
  7.   }
  8. ```

FAQ


How can I Control-click on results and have them open in a new tab?


You'll need to update your suggestion templates to make them as `` links
and not simple divs. Control-clicking on them will trigger the default browser
behavior and open suggestions in a new tab.

To also support keyboard navigation, you'll need to listen to the
autocomplete:selected event and change window.location to the destination
URL.

Note that you might need to check the value of context.selectionMethod in
autocomplete:selected first. If it's equal to click, you should return
early, otherwise your main window will also follow the link.

Here is an example of how it would look like:

  1. ``` js
  2. autocomplete().on('autocomplete:selected', function(event, suggestion, dataset, context) {
  3.   // Do nothing on click, as the browser will already do it
  4.   if (context.selectionMethod === 'click') {
  5.     return;
  6.   }
  7.   // Change the page, for example, on other events
  8.   window.location.assign(suggestion.url);
  9. });
  10. ```

Events


The autocomplete component triggers the following custom events.

autocomplete:opened – Triggered when the dropdown menu of the autocomplete is
  opened.

autocomplete:shown – Triggered when the dropdown menu of the autocomplete is
  shown (opened and non-empty).

autocomplete:empty – Triggered when all datasets are empty.

autocomplete:closed – Triggered when the dropdown menu of the autocomplete is
  closed.

autocomplete:updated – Triggered when a dataset is rendered.

autocomplete:cursorchanged – Triggered when the dropdown menu cursor is moved
  to a different suggestion. The event handler will be invoked with 3
  arguments: the jQuery event object, the suggestion object, and the name of
  the dataset the suggestion belongs to.

autocomplete:selected – Triggered when a suggestion from the dropdown menu is
  selected. The event handler will be invoked with the following arguments: the jQuery
  event object, the suggestion object, the name of the dataset the
  suggestion belongs to and a context object. The context contains
  a .selectionMethod key that can be either click, enterKey, tabKey or
  blur, depending how the suggestion was selected.

autocomplete:cursorremoved – Triggered when the cursor leaves the selections
  or its current index is lower than 0

autocomplete:autocompleted – Triggered when the query is autocompleted.
  Autocompleted means the query was changed to the hint. The event handler will
  be invoked with 3 arguments: the jQuery event object, the suggestion object,
  and the name of the dataset the suggestion belongs to.

autocomplete:redrawn – Triggered when appendTo is used and the wrapper is resized/repositionned.

All custom events are triggered on the element initialized as the autocomplete.

API


jQuery


Turns any input[type="text"] element into an auto-completion menu. globalOptions is an
options hash that's used to configure the autocomplete to your liking. Refer to
Global Options for more info regarding the available configs. Subsequent
arguments (*datasets), are individual option hashes for datasets. For more
details regarding datasets, refer to Datasets.

  1. ```
  2. $(selector).autocomplete(globalOptions, datasets)
  3. ```

Example:

  1. ``` js
  2. $('.search-input').autocomplete({
  3.   minLength: 3
  4. },
  5. {
  6.   name: 'my-dataset',
  7.   source: mySource
  8. });
  9. ```

jQuery#autocomplete('destroy')


Removes the autocomplete functionality and reverts the input element back to its
original state.

  1. ``` js
  2. $('.search-input').autocomplete('destroy');
  3. ```

jQuery#autocomplete('open')


Opens the dropdown menu of the autocomplete. Note that being open does not mean that
the menu is visible. The menu is only visible when it is open and has content.

  1. ``` js
  2. $('.search-input').autocomplete('open');
  3. ```

jQuery#autocomplete('close')


Closes the dropdown menu of the autocomplete.

  1. ``` js
  2. $('.search-input').autocomplete('close');
  3. ```

jQuery#autocomplete('val')


Returns the current value of the autocomplete. The value is the text the user has
entered into the input element.

  1. ``` js
  2. var myVal = $('.search-input').autocomplete('val');
  3. ```

jQuery#autocomplete('val', val)


Sets the value of the autocomplete. This should be used in place of jQuery#val.

  1. ``` js
  2. $('.search-input').autocomplete('val', myVal);
  3. ```

jQuery.fn.autocomplete.noConflict()


Returns a reference to the autocomplete plugin and reverts jQuery.fn.autocomplete
to its previous value. Can be used to avoid naming collisions.

  1. ``` js
  2. var autocomplete = jQuery.fn.autocomplete.noConflict();
  3. jQuery.fn._autocomplete = autocomplete;
  4. ```

Standalone


The standalone version API is similiar to jQuery's:

  1. ``` js
  2. var search = autocomplete(containerSelector, globalOptions, datasets);
  3. ```

Example:

  1. ``` js
  2. var search = autocomplete('#search', { hint: false }, [{
  3.   source: autocomplete.sources.hits(index, { hitsPerPage: 5 })
  4. }]);

  5. search.autocomplete.open();
  6. search.autocomplete.close();
  7. search.autocomplete.getVal();
  8. search.autocomplete.setVal('Hey Jude');
  9. search.autocomplete.destroy();
  10. search.autocomplete.getWrapper(); // since autocomplete.js wraps your input into another div, you can access that div
  11. ```

You can also pass a custom Typeahead instance in Autocomplete.js constructor:

  1. ``` js
  2. var search = autocomplete('#search', { hint: false }, [{ ... }], new Typeahead({ ... }));
  3. ```

autocomplete.noConflict()


Returns a reference to the autocomplete plugin and reverts window.autocomplete
to its previous value. Can be used to avoid naming collisions.

  1. ``` js
  2. var algoliaAutocomplete = autocomplete.noConflict();
  3. ```

Contributing & releasing



Credits


This library has originally been forked from Twitter's typeahead.js library.