Bootstrap Typeahead

is there a way to get bootstrap’s typeahead to only match the first characters of a word? Right now it’s matching any instance of the entered characters anywhere in each word, and I only want it to match the beginning.

For instance, if my array is

array("roof company", "company de roofing", "experts in roofing") 

and I type in “roof” I only want “roof company” to appear.

Does this make sense?

Hi there,

Yeah, sure you can do this.
As you probably know, can call the typeahead via JavaScript and use it’s different options to add functionality.
The option we’re interested in here is called matcher and it determines whether a query matches an item. It takes a single argument, i.e. the item against which to test the query. and the current query is accessed from within it via this.query. It returns a boolean true if the query is matched, its data type is function and by default it is case insensitive.

Therefore, what I would do, is take the array of subjects to compare against (e.g. [‘roof’, ‘roofer’, ‘a roof company’, ‘company de roofing’, ‘experts in roofing’]).
I would then split each element of the array at a space and compare it with the query. If you encounter a direct match, then you return true.

An example:

<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>Bootstrap typehead with options example by w3resource</title>
    <link href="" rel="stylesheet">
    <script src=""></script>
    <script src=""></script>
    <div class="well">
      <input type="text" class="span3" id="search" data-provide="typeahead" data-items="4" />
      var subjects = ['roof', 'roofer', 'a roof company', 'company de roofing', 'experts in roofing']; 
        source: subjects,
        matcher: function(item) {
          var words = item.split(" ");
          for(var i = 0, len = words.length; i < len; i++){
            if (words[i] === this.query){
              return true;

I hope that helps you.


Thanks for the help!