Anuja P
Anuja P

Reputation: 2143

Add and remove the bootstrap typeahead dynamically

Can we add and remove the bootstrap typeahead dynamically.

Means in some case I need to give the typeahaed functionality to one textbox and need to remove this functionality for the same input box in another case.

Upvotes: 9

Views: 14086

Answers (6)

dcarneiro
dcarneiro

Reputation: 7150

I had to use a different (and hackier) solution because I'm changing the typeahead data each time I bind it and couldn't get the other answers to work. I'll leave it here in case anyone needs it.

Remove Typeahead

var parent = $('.typeahead').parent();
var html = parent.html();
$('.typeahead).remove();
parent.html(html);

Add Typeahead

$('.typehead').typehead();

Upvotes: 1

Jason Kim
Jason Kim

Reputation: 19031

I found $('.some-typeahead-parent .tt-dropdown-menu').hide(); to work the best for multiple data set typeahead.

Upvotes: 0

thaviti
thaviti

Reputation: 21

This code works for me.

var typeaheadEle = $('#myTypeahead').data('typeahead');
 if (typeaheadEle) {typeaheadEle.source = [];}

where myTypeahead is id of the input element.

Upvotes: 1

Jeremy A. West
Jeremy A. West

Reputation: 2300

I know I'm late to the party, below will remove instead of just hide.

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

Upvotes: 10

Anuja P
Anuja P

Reputation: 2143

I have used the

 $('.typeahead').removeClass('dropdown-menu');

and

$('.typeahead').addClass('dropdown-menu');

to hide show that drop-down suggestion.

Upvotes: 2

Kasyx
Kasyx

Reputation: 3200

Yes, You can. It's quite easy with using jquery method .unbind()

For example:

$('.typehead').typehead();
//and then you can dynamically unbind this plugin
$('.typehead').unbind();

Upvotes: 6

Related Questions