Nikhil Bandivan

Nikhil Bandivan

  • 1.5k
  • 289
  • 3.1k

Clearing a Typeahead if no value is selected from the dropdo

Feb 2 2018 6:39 AM
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.
  1. $(function () {  
  2.     $('#txtCustomer').typeahead({  
  3.         hint: true,  
  4.         highlight: true,  
  5.         minLength: 1,  
  6.         source: function (request, response) {  
  7.             $.ajax({  
  8.                 url: '/Customer.aspx/GetCustomers',  
  9.             data: "{ 'prefix': '" + request + "'}",  
  10.             dataType: "json",  
  11.             type: "POST",  
  12.             contentType: "application/json; charset=utf-8",  
  13.             success: function (data) {  
  14.                 items = []; map = {};  
  15.                 var obj = JSON.parse(data.d);  
  16.                 $.each(obj.Data, function (i, value) {  
  17.                     map[name] = { id: value.CustomerID, name: value.FullName };  
  18.                     items.push(map[name]);  
  19.                 });  
  20.                 response(items);  
  21.             },  
  22.             error: OnError,  
  23.             failure: OnError,  
  24.         });  
  25.     },  
  26.     updater: function (item) {  
  27.         $('#hfCustomerId').val(item.id);  
  28.         return item;  
  29.     }  
  30. });  
  31. });