Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Flexible autocomplete component
Pull request Compare This branch is 16 commits behind matthewmueller:master.
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Autocomplete component



$ component install matthewmueller/autocomplete


<input type="search" id="search" name="keyword">
autocomplete(document.getElementById('search'), '/search/:keyword')
  .format(function(label, q) {
    var r = new RegExp('(?:' + q + ')', 'i');
    return label.replace(r, '<span class="highlight">$&</span>');
  .on('select', function(url) {
    img.src = url;


  • enabled autocomplete is now listening for user input
  • disabled autocomplete is no longer listening for user input
  • response (res) response object after running through autocomplete#parse
  • error (err) emitted when the server responds with an error
  • select (value) emitted when you select an item from the autocomplete menu


autocomplete(el, url, [opts])

Initialize a new Autocomplete instance. Pass in an input el, a url endpoint and some options.

url may take a single express-style parameter to be used as the query key. You can specify the query key by either setting the input[name] or calling autocomplete#key(...). If no query key is present, autocomplete will pass the query through as a querystring.

Available options include:

  • throttle : Defaults to 500. Throttles the user input to reduce the number of AJAX calls.
autocomplete(el, "")


Enables the autocomplete.



Disables the autocomplete.



The query key used as either part of the query string or express-style parameter. May also be set by adding a name attribute to the input el.

autocomplete.key('q') //


Displays the menu. display defaults to true. You may set this false if you just want to use the search feature without the autocomplete menu.



Parses the result object called immediately after a successful response from the server. This is useful for preparing the data. Many times you don't recieve a direct result set, but instead an object containing the results, maybe something like { result : [...] }. key may be a string or a function and supports to-function notation.


or as a function

autocomplete.parse(function(res) {
  return res.result;


Determines which key from the result object should be used to map label names in the menu. key may be a string or a function. label uses to-function, so you may use dot notation to traverse the result object.


or as a function

autocomplete.label(function(item) {


Determines which key from the result object should be used to map to values in the menu. This data is what will be passed when the user selects an item on the menu. key may be a string or a function and supports to-function notation.


or as a function

autocomplete.value(function(item) {
  return item.url;


Format the menu items. Pass a formatter(label, query) function in to modify menu items. Useful for styling the result, such as providing query highlighting.

Here's how to highlight the query within the menu

autocomplete.format(function(label, query) {
  var r = new RegExp('(?:' + q + ')', 'i');
  return label.replace(r, '<span class="highlight">$&</span>');


Call search to manually execute a search. This is usually called by default as you type, but may be useful for one-off searches or if you disabled autocomplete. Optionally you may pass an fn function to handle the results or listen for the response event. Please note that res is run through autocomplete#parse beforehand., res) {
  if(err) throw err;


Access to the raw menu component. Useful for listening to specific menu events.


Used to manually modify the position of the menu. May be useful for providing additional menu styling like carets and stuff. fn must return a position object with x and y keys.

Here's the default fn

autocomplete.position(function(el) {
  var coords = getOffset(el),
      x = coords.left,
      y = + el.offsetHeight;

  return { x : x, y : y };


$ make test
$ node test/server.js
$ open http://localhost:7000



Something went wrong with that request. Please try again.