Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the source Option in bootstrap-typeahead.js

I am using bootstrap-typeahead in order to allow multiple selection. Here is the demo.

The original code has been update by @Sherbrow Twitter bootstrap typeahead multiple values

My question is related to the following use case:
after inserting Alaska value, I would like to update the source not showing again Alaska value.
Any hints?

like image 349
Lorraine Bernard Avatar asked Nov 22 '12 16:11

Lorraine Bernard


People also ask

What happened to typeaheads in Bootstrap?

Typeahead plugin has been dropped from the Bootstrap (v3.0+), in favor of using Twitter typeahead. Twitter typeaheads is a fast and fully-featured autocomplete library inspired by twitter.com's autocomplete search functionality.

How to specify external dataset in bootstrap type-ahead dropdown?

Tip: Set autocomplete="off" for the input box if you want to prevent default browser menus from appearing over the Bootstrap type-ahead dropdown. You can also specify external dataset through a URL pointing to a JSON file containing an array of datums. The individual units that compose datasets are called datums.

What is type ahead in Bootstrap?

Creating Typeaheads with Bootstrap The typeahead input fields are very popular in modern web forms. The main purpose of using typeahead is to improve the user experience by supplying hints or a list of possible choices based on the text they've entered while filling a form or searching something — like the Google instant search.

How to prevent default browser menus from appearing over bootstrap type-ahead dropdown?

Tip: Set autocomplete="off" for the input box if you want to prevent default browser menus from appearing over the Bootstrap type-ahead dropdown. You can also specify external dataset through a URL pointing to a JSON file containing an array of datums.


2 Answers

I had the same problem and this one will save you a lot of time. I've updated your old jsFiddle with my code example. The basic thing is that you need to do

var autocomplete = $('input').typeahead();
autocomplete.data('typeahead').source = newSource;

Where newSource is the new array. Now you just need a function that adds or removes an element, or whatever you need to do with it.

like image 93
toxicate20 Avatar answered Oct 15 '22 12:10

toxicate20


None of the given answers worked for me, I had to destroy the original typeahead instance and re initialize it.

$('input').typeahead('destroy').typeahead(options);

like image 26
micahblu Avatar answered Oct 15 '22 11:10

micahblu