Clear typeahead field
Asked Answered
J

3

7

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("");
});
Juliajulian answered 5/1, 2015 at 14:6 Comment(2)
Seems to be working fine on my machine. Using jQ 2.0 and IE11 and Chrome 39. Please post your input element and other event that are attached to it.Velar
$("#field").on("blur", function() { $(this).val(""); }); ?Kandi
B
7

try with this, example here in fiddle

$('.typeahead').typeahead().bind('typeahead:close', function () {
    $('.typeahead').typeahead('val', '');
});
Broadminded answered 5/1, 2015 at 15:54 Comment(3)
Thanks for your reply,my solution is close to this one you passed: function clear(field{ $("#"+field).typeahead('val', ''); }Juliajulian
It seems the event is now 'typeahead:close' not 'typeahead:closed' not sure if this is a typo or whether it changed in later versions of typeahead - github.com/twitter/typeahead.js/blob/master/doc/…Crocus
This is outdated and no longer works. See https://mcmap.net/q/1447738/-clear-typeahead-fieldStood
D
3

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', '');
});
Diggins answered 25/8, 2017 at 12:29 Comment(0)
A
2

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', '')
});
Apc answered 25/3, 2019 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.