Rafael Roque
Rafael Roque

Reputation: 377

Clear typeahead field

how could I clear a typeahead field in a focusout event?

The following jQuery code doesn´t seem to work in a typeahead field:

$( "#field" ).focusout(function() {
    $(this).val("");
});

Upvotes: 7

Views: 16336

Answers (3)

Diego Rodriguez
Diego Rodriguez

Reputation: 120

try with this, example here in fiddle

$('.typeahead').typeahead().bind('typeahead:close', function () {
    $('.typeahead').typeahead('val', '');
});

Upvotes: 7

Gobinda Nandi
Gobinda Nandi

Reputation: 515

Thanks for all the answers, but the close event did not work for me maybe my setting or something has deprecated. so I have written this code and it worked well.

$('#typeahead-id').typeahead().bind('typeahead:selected', function() {
    $('#typeahead-id').typeahead('val', '')
});

Upvotes: 2

Brent Matzelle
Brent Matzelle

Reputation: 4093

The accepted answer is now out of date. See the latest Typeahead documentation to see that trapping the "close" event and setting the value of the input are different. Here's an equivalent, updated answer:

$('.typeahead').typeahead().bind('typeahead:close', function() {
    $('.typeahead').typeahead('val', '');
});

Upvotes: 3

Related Questions