Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I use typeahead.js as autocomplete textbox.

when I input and select a value from the suggestions, textbox and hiddenfield sets the value correctly. But when I input a value and loose focus of textbox without selecting textbox value and hiddenfield value are missmatch.

How do I clear the value of the textbox and hiddenfield when the input value is not selected from suggestions.

What I have tried:

$(function () {
    $('#txtCustomer').typeahead({
        hint: true,
        highlight: true,
        minLength: 1,
        source: function (request, response) {
            $.ajax({
                url: '/Customer.aspx/GetCustomers',
            data: "{ 'prefix': '" + request + "'}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                items = []; map = {};
                var obj = JSON.parse(data.d);
                $.each(obj.Data, function (i, value) {
                    map[name] = { id: value.CustomerID, name: value.FullName };
                    items.push(map[name]);
                });
                response(items);
            },
            error: OnError,
            failure: OnError,
        });
    },
    updater: function (item) {
        $('#hfCustomerId').val(item.id);
        return item;
    }
});
});
Posted
Updated 27-Jul-19 8:33am

How about adding the closed event at end like following:

JavaScript
.on("typeahead:closed",function() {
alert("Dropdown Closed!");
 $('#hfCustomerId').val('');
 $('#txtCustomer').val('');
});


Refer to the following fiddle:

Demo Fiddle
 
Share this answer
 
v2
Comments
Laxmidhar tatwa technologies 2-Feb-18 11:53am    
Yes in the above function the textbox should be cleated
nikhil8085 3-Feb-18 2:00am    
not working for me :(
Ehsan Sajjad 3-Feb-18 3:14am    
Check the browser console for any js errors
nikhil8085 3-Feb-18 4:54am    
not getting any errors.
nikhil8085 3-Feb-18 4:55am    
My updated code But no luck for me

$(function () {
$('#txtCustomer').typeahead({
hint: true,
highlight: true,
minLength: 1,
source: function (request, response) {
$.ajax({
url: '/Customer.aspx/GetCustomers',
data: "{ 'prefix': '" + request + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
items = []; map = {};
var obj = JSON.parse(data.d);
$.each(obj.Data, function (i, value) {
map[name] = { id: value.CustomerID, name: value.FullName };
items.push(map[name]);
});
response(items);
},
error: OnError,
failure: OnError,
});
},
updater: function (item) {
$('#hfCustomerId').val(item.id);
return item;
}
});
}).on("typeahead:closed",function() {
alert("Dropdown Closed!");
$('#hfCustomerId').val('');
$('#txtCustomer').val('');
});
use "typeahead:close" instead of "typeahead:closed"
see typeahead.js/jquery_typeahead.md at master · twitter/typeahead.js · GitHub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900