Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Typeahead

Bootstrap Typeahead

Introduction

In this tutorial, you will see how to create a Typeahead with Bootstrap.

Typeahead provides hints or data to users as they fill out a form.

Bootstrap allows you to create good-looking Typeaheads. Moreover, it is easy to extend, so you can enhance its functionality without major changes.

What is required

Typeahead is a product of Bootstrap 2.x, so this tutorial uses Bootstrap 2.0 for testing.

You must reference two JavaScript files, jquery.js and bootstrap-typeahead.js, both located in docs/assets/js/.

The following example shows how to create a Typeahead using only markup, without writing JavaScript.

Example

Example

    

You can call typeahead via JavaScript and enhance its functionality using different options. The usage of calling typeahead via JavaScript is as follows:

$('#example').typeahead()

where 'example' is the id of the input field to which you want to apply typeahead.

Example

Example

    

Option

You can use various Typeahead options. Each option is explained in detail below:

source: Specifies the data source containing the values to display when querying. The value is of type array, with a default of [ ].

items: Specifies the maximum number of items to display when querying. The data type is number, with a default of 8.

matcher: Determines whether a query matches an item. It takes a single parameter, the item against which to test the query. The current query is accessed via this.query. It returns a boolean true if the query matches. The data type is function. By default, it is case-insensitive.

sorter: Used to automatically sort the results. It takes a single parameter, the item with the scope of the typeahead instance. The current query is accessed via this.query. The data type is function. The default is exact match; other values can be case-sensitive or case-insensitive.

highlighter: Used to automatically highlight the results. It takes a single parameter, the item with the scope of the typeahead instance. The data type is function. By default, it highlights all default matching items.

Method



.typeahead(options)

It initializes an input field with Typeahead.

Click here to download all the HTML, CSS, JS, and image files used in this tutorial.