24

I'm trying to use Twitter Typeahead with Bootstrap 3 RC1, because Bootstrap dropped its own typeahead plugin on version 3.

I'm using the following HTML on my form:

<div class="well">  
<form>
    <fieldset>
        <div class="form-group">
            <label for="query">Search:</label>
            <input type="text" class="form-control" name="query" id="query" placeholder="Start typing something to search...">              
        </div>
        <button type="submit" class="btn btn-primary">Search</button>
    </fieldset>
</form>
</div>

Without adding Typeahead, the search input field displays like this:

before adding Typeahead

Then I added Typeahead like this:

<script>
    $('#query').typeahead({        
        local: ['alpha', 'bravo','charlie','delta','epsilon','gamma','zulu']
    });
</script>

And the field became smaller, with a white rectangle inside it.

after adding Typeahead

when typing some text...

Am I doing something wrong or it's just a bug on Typeahead or Bootstrap 3 RC1?

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
javsmo
  • 1,337
  • 1
  • 14
  • 23
  • 1
    possible duplicate of [CSS issue on Twitter Typeahead with Bootstrap 3](http://stackoverflow.com/questions/18059161/css-issue-on-twitter-typeahead-with-bootstrap-3) – Hieu Nguyen Aug 11 '13 at 10:28
  • 1
    Although support has been dropped you can use the "old" plugin https://github.com/bassjobsen/Bootstrap-3-Typeahead – Bass Jobsen Sep 18 '13 at 12:29

5 Answers5

39

update 14 feb 2014

As mentioned by laurent-wartel try https://github.com/hyspace/typeahead.js-bootstrap3.less or https://github.com/bassjobsen/typeahead.js-bootstrap-css for additional CSS to use typeahead.js with Bootstrap 3.1.0.

Or use use the "old" (TB 2) plugin with the new Bloodhound suggestion engine: https://github.com/bassjobsen/Bootstrap-3-Typeahead/issues/26


Twitter's typeahead doesn't seem ready for Twitter's Bootstrap 3. To use Twitter's typeahead with Twitter's Bootstrap you will need some extra CSS (deprecated, no longer maintained). Using this CSS doesn't fix your problems.

In your example the input field don't get a 100% width. This will be cause by the Javascript. The javascript wraps the input in a span:

<span class="twitter-typeahead" 
    style="position: relative; display: inline-block; direction: ltr;">

This span don't got a 100% so do input inside it. To fix add to your CSS:

.twitter-typeahead {
  width: 100%;
}

The Javascript sets the background-color of your input to transparent. If you don't want to change the plugin source i will need some additional Javascript to fix this:

$('.tt-query').css('background-color','#fff');

Now is should work as expected based on your example. http://www.bootply.com/73439

update

The original question was for RC1 for Twitter's Bootstrap 3.0.0. you also need to add:

.tt-dropdown-menu {
   width:100%;        
}

New bootply: http://bootply.com/86305

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • 6
    In addition, you might want to add .tt-hint to that CSS, otherwise the hint is cut off at the original width, so: `.twitter-typeahead, .tt-hint { width: 100%; }` – Darren Shewry Oct 02 '13 at 10:38
2

Have a look at typeahead.js-bootstrap3.less.

It works great with the latest version of Bootstrap (v3.0.3) and allows you to keep your custom theming. There is also a .css file with default bootstrap theming.

Laurent W.
  • 3,629
  • 2
  • 20
  • 29
1

Typeahead.js css fixing also have problems with input groups (corners rounding), i found somewhere info that it breaks on modals, etc. Additionaly https://github.com/jharding/typeahead.js-bootstrap.css is no longer mantained so i am going to give a try for Bass Jobsen solution ;)

3176243
  • 561
  • 4
  • 7
  • 1
    After spending a few hours best replacement for me is https://github.com/biggora/bootstrap-ajax-typeahead (for ajax json arrays), nicely working with bs 3.0.3. I will need to check css fixing, but should work easily as dropdown generation templates are easily accesible. (of course .typeahead.dropdown-menu { width: 100% } is required :D) there is 1 error in examples for ajax, showLoadingMask don't work (BS 2 artifact?) unfortunately no caching for requests yet ;) – 3176243 Jan 09 '14 at 08:10
0

After a lot of research with no luck at all adding styles, I finally fixed it adding one line inside the constructor Typeahead (bootstrap.js or bootstrap-typeahead.js depending on your version):

this.$menu.width(this.$element.outerWidth());

Here's the code:

/* TYPEAHEAD PUBLIC CLASS DEFINITION
 * ================================= */

var Typeahead = function (element, options) {
    this.$element = $(element)
    this.options = $.extend({}, $.fn.typeahead.defaults, options)
    this.matcher = this.options.matcher || this.matcher
    this.sorter = this.options.sorter || this.sorter
    this.highlighter = this.options.highlighter || this.highlighter
    this.updater = this.options.updater || this.updater
    this.source = this.options.source
    this.$menu = $(this.options.menu)
    this.shown = false
    this.listen()
    this.$menu.width(this.$element.outerWidth());
}
lukiller
  • 1,107
  • 9
  • 12
-2

Instead Twitter Typeahead, try to use bootstrap-select. This library is integrated into the Bootstrap 3 and has a wider functionality.