Search

Search lets users specify a word or phrase to search for relevant pieces of information. Search can be used as a filter or a primary means of searching some content.


Basic Example

The basic style for search component that can be submitted by hitting enter kay or by clicking the search icon on the right side of the bar.

<div class="search-form">
  <input type="search" class="form-control" placeholder="Search">
  <button class="btn" type="button"><i data-feather="search"></i></button>
</div>

Expanding Search Bar

A search input that can be expanded when clicking the search icon and automaticall minimize when focusing outside of the input box.

<form id="searchExpanding" class="search-form search-form-expanding">
  <input type="search" class="form-control" placeholder="Search">
  <button class="btn" type="button"><i data-feather="search"></i></button>
</form>
$('#searchExpanding button').on('click', function(e){
  e.preventDefault()

  if($(this).prev().val() === '') {
    $(this).parent().toggleClass('expand');
  } else {
    $(this).parent().submit();
  }
})

// Collapse expanding search
$('#searchExpanding .form-control').on('focusout', function(e){
  if($(this).val() === '') {
    $(this).parent().removeClass('expand');
  }
})

With Typeahead

When initializing a typeahead using the typeahead.js jQuery plugin, you pass the plugin method one or more datasets. The source of a dataset is responsible for computing a set of suggestions for a given query. Read the Official Typeahead Documentation for a full list of instructions and other options.

<div class="search-form">
  <input id="theBasics" type="search" class="form-control" placeholder="Search States of USA">
</div>
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
  var matches, substrRegex;

  // an array that will be populated with substring matches
  matches = [];

  // regex used to determine if a string contains the substring `q`
  substrRegex = new RegExp(q, 'i');

  // iterate through the pool of strings and for any string that
  // contains the substring `q`, add it to the `matches` array
  $.each(strs, function(i, str) {
    if (substrRegex.test(str)) {
      matches.push(str);
    }
  });

  cb(matches);
};
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

$('#theBasics').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: substringMatcher(states)
});

Bloodhound

For more advanced use cases, rather than implementing the source for your dataset yourself, you can take advantage of Bloodhound, the typeahead.js suggestion engine.

var states = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // `states` is an array of state names defined in "The Basics"
  local: states
});

$('#bloodhound').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: states
});

Prefetch

Prefetched data is fetched and processed on initialization. If the browser supports local storage, the processed data will be cached there to prevent additional network requests on subsequent page loads.

var countries = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // url points to a json file that contains an array of country names, see
  prefetch: '../data/countries.json'
});

// passing in null for the options arguments will result in the default
// options being used
$('#prefetch').typeahead(null, {
  name: 'countries',
  source: countries
});

Remote

Remote data is only used when the data provided by local and prefetch is insufficient. In order to prevent an obscene number of requests being made to the remote endpoint, requests are rate-limited.

var bestPictures = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/films/post_1960.json',
  remote: {
    url: '../data/films/queries/%QUERY.json',
    wildcard: '%QUERY'
  }
});

$('#remote').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures
});

Custom Template

Custom templates give you full control over how suggestions get rendered making it easy to customize the look and feel of your typeahead.

$('#custom ').typeahead(null, {
  name: 'best-pictures',
  display: 'value',
  source: bestPictures,
  templates: {
    empty: [
      '<div class="empty-message">',
        'Unable to find any best picture winners that match the current query',
      '</div>'
    ].join('\n'),
    suggestion: Handlebars.compile('<div><strong>{{value}}</strong> – {{year}}</div>')
  }
});

Multiple Datasets
var nbaTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/nba.json'
});

var nhlTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../data/nhl.json'
});

$('#datasets').typeahead({
  highlight: true
},
{
  name: 'nba-teams',
  display: 'team',
  source: nbaTeams,
  templates: {
    header: '<h3 class="league-name">NBA Teams</h3>'
  }
},
{
  name: 'nhl-teams',
  display: 'team',
  source: nhlTeams,
  templates: {
    header: '<h3 class="league-name">NHL Teams</h3>'
  }
});

Scrollable Dropdown
$('#scrollDrop').typeahead(null, {
  name: 'countries',
  limit: 10,
  source: countries
});