Troubleshooting Problem with Twitter Typeahead and Bootstrap 3 CSS

Bootstrap 3 has removed Typeahead in favor of the library found at https://github.com/twitter/typeahead.js. I have successfully integrated it for remote data fetching, but am facing issues with autocompletion. Despite including the necessary CSS from https://github.com/jharding/typeahead.js-bootstrap.css, the autocompletion is not working as expected.

Any help or suggestions would be greatly appreciated. You can view the issue on the following JSFiddle link: http://jsfiddle.net/KrtB5/

HTML

<body>
    <div class="container">
        <label>State</label> <input type="text" class="typeahead form-control" />
    </div>
</body>

Javascript:

$('.typeahead').typeahead({
    name: 'Some name',
    local: ['test', 'abc', 'def']
})

Answer №1

UPDATE: Tweaked for Bootstrap 3.0 UPDATE 2: Adjusted Typeahead call. Refer to the new jsfiddle link below.

Upon experimenting with the styling, I noticed that the form-control class does not perfectly align with the tt-hint. Therefore, I made adjustments to ensure that the margins and borders are in sync. Building on Hieu Nguyen's solution, I incorporated border-radius properties and included support for both input-small and input-large options.

CSS Styles:

.twitter-typeahead .tt-hint
{
    display: block;
    height: 34px;
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.428571429;
    border: 1px solid transparent;
    border-radius:4px;
}

.twitter-typeahead .hint-small
{
    height: 30px;
    padding: 5px 10px;
    font-size: 12px;
    border-radius: 3px;
    line-height: 1.5;
}

.twitter-typeahead .hint-large
{
    height: 45px;
    padding: 10px 16px;
    font-size: 18px;
    border-radius: 6px;
    line-height: 1.33;
}

Script for input-small and input-large:

$('.typeahead.input-sm').siblings('input.tt-hint').addClass('hint-small');
$('.typeahead.input-lg').siblings('input.tt-hint').addClass('hint-large');

Check out the updated demo on jsfiddle: http://jsfiddle.net/KrtB5/542/

Answer №2

It appears that the .form-control is a newly introduced class in Bootstrap 3 RC and seems to be causing this issue (which is expected as RC versions often have many issues). To resolve this, you can simply replicate the style of that class for the .tt-hint class. Here's how:

.twitter-typeahead .tt-hint {
    display: block;
    height: 38px;
    padding: 8px 12px;
    font-size: 14px;
    line-height: 1.428571429;
    border: 1px solid transparent;
}

Here is a working fiddle demonstrating the solution: http://jsfiddle.net/KrtB5/2/

Update: I have made an improvement that works better with jQuery 1.9.1 and Bootstrap 3.0.0: http://jsfiddle.net/KrtB5/13

Answer №3

Here's something interesting for you:

$('#foo').typeahead(...);
$('.tt-hint').addClass('form-control');

Answer №4

For those interested, there is also an unauthorized adaptation of the Bootstrap 2 typeahead plugin available:

Check out Bootstrap 3 Typeahead here

This version does not need any extra CSS files and is fully compatible with the most recent edition of Bootstrap.

If you want to see a demonstration, click on this demo link for Plunker.

Answer №5

If you're working with bootstrap.less, the process is much simpler. Bootstrap 3 comes with LESS 1.4.1 which now includes the 'extend' feature. Check out this tutorial on using a span3 (or any spanX number) class as a mixin in Less and Bootstrap.

'Extend' is a powerful tool in LESS that allows for full inheritance of explicitly named classes. You no longer need to manually copy properties like in Hieu Nguyen's and Nick P's CSS solutions. The following code snippet demonstrates how LESS takes care of it all:

.twitter-typeahead .tt-hint:extend(.form-control all)
{}

The original typeahead.js-bootstrap.less code on GitHub is outdated for Bootstrap 3. I used it as a foundation but also tweaked it to prevent word wrapping in the dropdown list, similar to BS 2 typeahead. Here's my finalized less file:

.tt-dropdown-menu
{
    /* CSS styles go here */
}

.tt-suggestion
{
    /* Additional styling for suggestions */
}

    .tt-suggestion.tt-is-under-cursor
    {
        /* Styling for cursor hover state */
    }

        .tt-suggestion.tt-is-under-cursor a
        {
            /* More styling for anchor elements */
        }

    .tt-suggestion > p
    {
        /* Paragraph styling */
    }

/* Additional CSS rules */

.twitter-typeahead
{
    /* Styling for Twitter typeahead */
}

.twitter-typeahead .tt-hint:extend(.form-control all)
{
    /* Custom styling for hint text */
}

Answer №6

The issue is not limited to just .tt-hint being broken, but also affects other CSS classes.

To resolve this problem and ensure compatibility across all browsers, it is recommended to include additional CSS that addresses the conflicts between Typeahead.js and Bootstrap 3:

https://github.com/jharding/typeahead.js-bootstrap.css

Answer №7

A detailed resolution (as suggested in this issue discussion on Typeahead)

https://github.com/hyspace/typeahead.js-bootstrap3.less/blob/master/typeahead.css


/*
 * typehead.js-bootstrap3.less
 * @version 0.2.3
 * https://github.com/hyspace/typeahead.js-bootstrap3.less
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */
.has-warning .twitter-typeahead .tt-input,
.has-warning .twitter-typeahead .tt-hint {
  border-color: #8a6d3b;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.has-warning .twitter-typeahead .tt-input:focus,
.has-warning .twitter-typeahead .tt-hint:focus {
  border-color: #66512c;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #c0a16b;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #c0a16b;
}
.has-error .twitter-typeahead .tt-input,
.has-error .twitter-typeahead .tt-hint {
  border-color: #a94442;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.has-error .twitter-typeahead .tt-input:focus,
.has-error .twitter-typeahead .tt-hint:focus {
  border-color: #843534;
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #ce8483;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 6px #ce8483;
}
...

Answer №8

After some trial and error, I managed to make it work. Adjusting the top and left values may be necessary for your specific case.

$('#typeahead').typeahead(...);
$(".tt-hint").css('top','3px');
$(".tt-hint").css('left','1px');

Answer №9

After considering Andreas's input, I would recommend the following approach to optimize usage:

.tt-hint { 
   .form-control;
}

Answer №10

To solve the issue, I decided to enhance my form-group element by including an additional CSS class called "form-group-lg".

Here is a snippet of my HTML code:

<div class="form-group form-group-lg">
  <label class="control-label" for="my-large-typeahead">Type to autocomplete:</label>
  <input type="text" class="form-control typeahead" id="my-large-typeahead">
</div>

In my SCSS file, I made the following adjustment:

.form-group-lg .tt-hint
{
    @extend .input-lg;
}

Answer №11

According to a post from Typeahead problems with Bootstrap 3.0 RC1, [laurent-wartel][2] suggested trying out additional CSS options like https://github.com/hyspace/typeahead.js-bootstrap3.less or https://github.com/bassjobsen/Bootstrap-3-Typeahead to integrate typeahead.js with Bootstrap 3.1.0.

Alternatively, you can also use the "old" (TB 2) plugin in combination with the new Bloodhound suggestion engine by referring to https://github.com/bassjobsen/Bootstrap-3-Typeahead/issues/26

Answer №12

A streamlined solution for a neater code

.tt-small {

    .twitter-typeahead {

        display: block !important; // Important: Override inline styles set by JavaScript

        &> .tt-hint {

            &:extend(.form-control);

            color: @medium-gray;

        }
    }
}

Here is an example of how the HTML markup might look:

<div class="form-group">
    <label class="col-lg-3 col-sm-3 control-label" for="mydropdown">Dropdown</label>
    <div class="col-lg-6 col-sm-6 tt-mydropdown tt-small">
        <input class="form-control" id="mydropdown" placeholder="Dropdown" type="text">
    </div> <!-- tt-small end -->
</div>

Answer №13

Here is an alternative method to integrate Twitter Typeahead with Bootstrap 3.

// To ensure our stylesheets can override inline styles from Twitter Typeahead, we use jQuery to remove them.
$('.twitter-typeahead, .typeahead').attr('style',''); 

Next, in your LESS stylesheet, include the following:

// Styles for Twitter Typeahead

.twitter-typeahead {
  position: relative;

  .tt-hint {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: @input-bg;
    border: none;
  }

  .tt-input {
    position: relative;
    vertical-align: top;
  }

  .tt-hint + .tt-input {
    background-color: transparent;
  }

  .tt-dropdown-menu {
    &:extend(.dropdown-menu all);
  }

  .tt-suggestion {
    &:extend(.dropdown-menu > li > a all);
    p {
      margin-bottom: 0;
    }
  }

  .tt-cursor {
    &:extend(.dropdown-menu > .active > a all);
  }

}

Answer №14

Skip the hassle of using complex setups, simply include

style="position: relative"

In the parent element. It's utilizing absolute positioning, it only requires clarification on which "absolute" you mean.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Import data into Bootstrap table from an external source

I am having trouble styling the table loaded from the table.html file onto the index page. Even after loading the table, the styles from bootstrap classes are not applied. What could be causing this issue? Importing bootstrap libraries directly into the ta ...

A step-by-step guide for efficiently transmitting two JSON arrays via AJAX to be displayed in Django

I am currently implementing ajax to pass a JSON array to the view in Django. On my index page, I have two columns - left and right. Each column contains several text inputs, and there is a 'Save' button that saves all the values from both sides i ...

Retrieving information from a table row and using it as a parameter in the URL for an AJAX request

New to implementing ajax, JQuery, and Json in my strut2 web application. I have a table on one web page with records displayed, and the last column contains an image as shown below. Currently using an anchor tag with an image for the last column with an ac ...

Building a scrollable dropdown menu with Bootstrap 5: A step-by-step guide

The search for suitable code examples for scrollable dropdown menus compatible with Bootstrap 5 has been challenging. Are there any contemporary methods available to achieve this functionality seamlessly? ...

Navigate through the overlay div to access the canvas, with a delegated click event triggered by the coordinates

Currently, my canvas application does not have a built-in zoom and pan feature. To work around this limitation, I have created an overlay on top of the canvas with pointer-events: all and opacity: 0, allowing users to zoom and pan seamlessly. However, I ...

Tips for displaying a title within the border of a div

We have a client who is asking for a unique feature on their new website - they want the title to appear within the border of a text area. We need help figuring out how to achieve this using CSS/HTML. Take a look at the effect we are trying to create: htt ...

Conflicting Joomla Modules

I'm currently working on a project at the following link: www.eltotaldesign.com/planeteco Within this project, I have integrated the modules DJ Image Slider and Altra Switcher. Despite attempting to install Easy JQuery and other methods, I have been ...

Adding the CSS property overflow:hidden seems to be causing issues with the functionality of my drop

I am currently developing my own personal news website called News Pews, which happens to be my third project. Interestingly, I did not encounter this particular issue in my previous projects. The webpage I'm working on has a horizontal scroll (x-scr ...

Show the JSON data from the server in a table format

Is there a way to neatly display data received from an Ajax response within a table format? Below is the structure of the table: <table data-toggle="table" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" id="menu_table"> ...

Set the status to 0 when the user exits the browser

I am currently working on developing a straightforward system to indicate whether the user is online or not. However, I have encountered a specific issue. Once the user closes the tab or the browser, the ajax code fails to refresh the page that tracks use ...

Assorted dimensions of square grid

Is it feasible to achieve the following using only HTML and CSS? The condition is that since the divs are automatically generated in a list, they should not be nested within another HTML element (such as adding a div for each line is restricted): Here is ...

Changing the source of a jQuery autocomplete dynamically without the need for conditional statements

Inputs: "Room number", "Name". The Autocomplete feature of the second input field is dynamically changing based on the value of the first input field. Is there a way to simplify my lengthy "if-else" conditions into a shorter and more automatic script? W ...

Is there a way to specifically select the input field that the user has typed into using a selector?

Is it possible to use javascript/jquery to specifically target the input field that a user has just typed in? ...

Ensuring the container height remains consistent with fluctuating content dimensions

Imagine a container with content, where the container's width is fixed but its height adjusts based on its inner content. Initially, when the content causes the container to be a specific height, the challenge arises in shrinking the inner elements w ...

Troubleshooting error in WordPress: Changing innerHTML of dynamically created divs using JavaScript. Issue: 'Unable to set property innerHTMl of null'

I am struggling to modify the innerHTML of a "View cart" button using a dynamically generated div class on my Wordpress/Woocommerce site. In a previous inquiry, I was informed (thanks to Mike :) ) that since JavaScript is an onload event, the class changes ...

Is there a way to verify if an ID includes more than one word?

I am trying to target a specific div with a unique id in jQuery: <div id="picture_contents_12356_title"></div> The '12356' is autogenerated and must be included in the id. I need to create a jQuery selector that combines "picture_co ...

What is the best method to implement nanoscroller for jQuery datatables within the table body?

I am trying to implement the nanoscroller feature in my table using data tables. My goal is to have a fixed header with scrolling only enabled for the table body, but currently the scroll applies to the entire table. If anyone has experience with this iss ...

Send a nested object as part of the POST request

Is there a way to send a nested object using a POST request? let name = "test", path = "?diffCurr%5B%5D=FALSE&diffProv%5B%5D=FALSE", data = { datatype:"report", "value":{ "name":name, "query":pat ...

Animating transitions on a dynamic Bootstrap navbar using jQuery

I have the code attached here, and I successfully changed the color of the navbar and logo when scrolling down. However, I am looking to add a transition effect when this change occurs. I experimented with various transition options in CSS, but unfortunat ...

Steps for modifying the width of a horizontal line element so that it aligns with an input element

I'm attempting to dynamically adjust the hr element's length based on the length of the input element by using CSS. Additionally, I'd like to incorporate an animation that triggers when the input is focused. I came across a solution for th ...