jQuery.spotlite

simple input autofill/search

by Cameron Daigle

jQuery.spotlite

Usage

Call $.spotlite() on the element (usually an <li> or <dd>) containing your text input. Spotlite will position a list of search results directly after the text input.

Requirements

Include jQuery and jquery.spotlite.js, then style the output however you like (or feel free to pull the styles from the demo).

Options

Features

The best way to see the breadth of what Spotlite can handle is to check out the test suite.

However, Spotlite's most interesting feature deserves a quick example: the ability to search through an array of Javascript objects. This is tremendously useful in situations where you want to spice up your search results (with images or specific CSS classes, for example) or create hidden inputs to pass back to the server for form submission.

Here's a contrived example. Say your pool is full of objects like this:

{
  name: "Frances McDormand",
  location: "San Diego Sports Arena",
  image: "/images/frances.jpg"
}

In this case, you'll probably want to search on name only, and display the location and thumbnail image in your dropdown menu & results. Just set the bypass option to "location, image", and redefine the output function to something like this:

output: function(e) {
  return $("<li />")
    .text(e.name)
    .append($("<small />", { src: e.location })
    .append($("<img />", { "src": e.image }));
}

This is just a basic example - the ability to directly control all of your users' matches and results gives you plenty of design flexibility.

New in 0.2.1: Now handles multi-layered JSON objects, thanks to none other than Durran Jordan of Mongoid fame.

New in 0.3: Now supports Ajax calls for search results and can be called on a select tag instead of an input with JSON.

Questions & Comments

We use Spotlite in a number of projects at Hashrocket, and I'm working on an extended article detailing why & how Spotlite came about. In the meantime, feel free to contact me with questions or feature requests (I'll politely answer your questions and (probably) politely decline your feature requests).