title: Jquery ui autocomplete with barcode scanner, text is deleted on focusout
body: <p>I have two input fields that come one after another:</p>
  
  <pre><code>&lt;input class="inputItemNum ui-autocomplete-input" id="tbAutoItemCode1" name="tbAutoItemCode1" type="text" value="" indexer="1" onkeydown="return (event.keyCode!=13);" autocomplete="off"&gt;
  &lt;input class="inputItemName ui-autocomplete-input" id="tbAutoItemName1" name="tbAutoItemName1" type="text" value="" indexer="1" onkeydown="return (event.keyCode!=13);" autocomplete="off"&gt;
  </code></pre>
  
  <p>The first represents an item code and the other represents an item name.
  On both of them I am using jquery-ui autocomplete and in 2 situations:
  1) on input
  2) on focus out</p>
  
  <p>I need the autocomplete <code>on focusout</code> because I am using an android device with bar-code scanner that inserts two Tab characters as input suffix, so when reading input into the item num it does the autocomplete and when leaving the input element (with one of the tab suffix) it checks if the item num is ok (again with autocomplete - because the user might entered manually an item code that does not exists- in that case I will clean the input fields.)</p>
  
  <p>Also need to mention that my autocomplete list comes from an ajax request.
  The problem I am facing is that when reading the item num with the bar-code scanner it also fills in the item name (as expected) but sometimes it deletes the item name the second it was filled.</p>
  
  <p>I thought it was related to the async ajax request (when the response comes back the tab suffix was already inserted and the fields were cleaned) so I tries using the ajax <code>async:false</code> but still no luck there. I hope someone can help me put.
  here is my javascript code that uses the autocomplete:</p>
  
  <pre><code>$(document).on('input', '.inputItemNum', function () {
  
      var inputId = $(this).attr("id");
      var text = $(this).val();
      var index = $(this)[0].attributes["indexer"].value;
      if (text.length &gt; 0) {
          $(this).autocomplete({
              //Alina 22-09-2016  after press a key, it takes 0ms(by default: 300ms) before it re-evaluates the dataset.
              delay: 100,
              focus: function () {
                  $(".autoCompleteItemCode").autocomplete("option", "delay", 0);
              },
              source: function (request, response) {
  
                  $.ajax({
                      url: "Services/FacAjax.asmx/getCompletionItemList",
                      data: JSON.stringify({ "prefixText": text, "count": 40, 'mode': 'code' }),
                      dataType: "json",
                      type: "POST",
                      async: true,
                      contentType: "application/json; charset=utf-8",
                      dataFilter: function (data) { return data; },
                      success: function (data) {
                          response($.map(data.d, function (item) {
                              // console.log(item);
                              return {
                                  value: item.ItemCode,
                                  code: item.ItemCode,
                                  name: item.ItemName,
                                  price: item.ItemPrice,
                                  multiple: item.Multiple,
                                  vat: item.Vat,
                                  discount: item.Discount
                              }
  
                          }))
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          //alert(errorThrown);
                          return false;
                      }
                  });
              },
              minLength: 1,
              autoFocus: true,
              select: function (event, ui) {
  
                  $("#tbAutoItemCode" + index).val(ui.item.code);
                  $("#tbAutoItemName" + index).val(ui.item.name);
                  $("#tbAutoItemPrice" + index).val(numberToCurrency(ui.item.price));
                  $("#tbAutoItemMultiple" + index).val(ui.item.multiple);
  
                  var l_DiscPrcnt = Math.round(ui.item.discount * 100) / 100;
  
                  $("#tbAutoItemDiscountPercent" + index).val(l_DiscPrcnt);
  
                  //round the display of discount percent to two decimal places
                  var l_nAfterDiscount = (ui.item.price * ui.item.multiple) - ((ui.item.price * ui.item.multiple) * (ui.item.discount / 100));
                  l_nAfterDiscount = Math.round(l_nAfterDiscount * 100) / 100;
                  $("#tbAutoItemAfterDiscount" + index).val(l_nAfterDiscount);
                  $("#tbVatPer" + index).val(ui.item.vat);
                  $("#tbItemVat" + index).val(l_nAfterDiscount * ui.item.vat / 100);
                  $("#tbAutoItemComments" + index).click(function () {
                      //openItemDescription(ui.item.code, ui.item.name)
                      return false;
                  });
  
                  reCalculateRow(index);
                  event.preventDefault();
                  //event.stopPropogation();
                  //focusQtyColumn(index);
                  focusItemNameColumn(index);
                  $(".ui-autocomplete").hide();
                  //addRow();
                  return false;
              }
          });
  
      } // if length &gt; 0
  
  
  }); //keyup
  
    $(document).on('keydown', '.inputItemNum', function (event) {
          var code = event.keyCode || event.which;
          if (code == '9') {
              //alert("Tab Pressed");
              event.stopPropagation();
              return;
          }
  
    });
  
  
  $('.inputItemNum').live("focusout", function () {
      var index = $(this)[0].attributes["indexer"].value;
      if ($(this).val() == "") { return; };
  
      //console.log("Lost focus from indexer " + indexer + " with value " + $(this).val());
      $.ajax({
          url: "Services/FacAjax.asmx/getCompletionItemList",
          data: "{ 'prefixText': '" + $(this).val() + "','count': '1','mode':'exact_code'}", //search for exact code!
          dataType: "json",
          type: "POST",
          async: true,
          contentType: "application/json; charset=utf-8",
          dataFilter: function (data) { return data; },
          success: function (dataArr) {
  
              if (dataArr.d.length == 0) {
                  // $("#tbAutoItemCode" + indexer).val("");
                  clearRowData(index);
                  reCalculateRow(index);
                  return;
              }
              var data = dataArr.d[0];
  
              $("#tbAutoItemCode" + index).val(data.ItemCode);
              $("#tbAutoItemName" + index).val(data.ItemName);
              $("#tbAutoItemPrice" + index).val(numberToCurrency(data.ItemPrice));
              $("#tbAutoItemMultiple" + index).val(data.Multiple);
  
              var l_DiscPrcnt = Math.round(data.Discount * 100) / 100;
  
              $("#tbAutoItemDiscountPercent" + index).val(l_DiscPrcnt);
  
              //round the display of discount percent to two decimal places
              var l_nAfterDiscount = (data.ItemPrice * data.Multiple) - ((data.ItemPrice * data.Multiple) * (data.Discount / 100));
              l_nAfterDiscount = Math.round(l_nAfterDiscount * 100) / 100;
              $("#tbAutoItemAfterDiscount" + index).val(l_nAfterDiscount);
              $("#tbVatPer" + index).val(data.Vat);
              $("#tbItemVat" + index).val(l_nAfterDiscount * data.Vat / 100);
              $("#tbAutoItemComments" + index).click(function () {
                  openItemDescription(data.ItemCode, data.ItemName)
                  return false;
              });
  
              reCalculateRow(index);
              focusItemNameColumn(index);
              //focusQtyColumn(index);
              $(".ui-autocomplete").hide();
              return false;
          }
  
      })
  }); //focusout
  
  $(document).on('input', '.inputItemName', function () {
  
      var inputId = $(this).attr("id");
      var text = $(this).val();
      var index = $(this)[0].attributes["indexer"].value;
      if (text.length &gt; 0) {
          $(this).autocomplete({
              //Alina 22-09-2016  after press a key, it takes 0ms(by default: 300ms) before it re-evaluates the dataset.
              delay: 0,
              focus: function () {
                  $(".autoCompleteItemCode").autocomplete("option", "delay", 0);
              },
              source: function (request, response) {
  
                  $.ajax({
                      url: "Services/FacAjax.asmx/getCompletionItemList",
                      data: JSON.stringify({ 'prefixText': text, 'count': 40, 'mode': 'name' }),
                      dataType: "json",
                      type: "POST",
                      async: true,
                      contentType: "application/json; charset=utf-8",
                      dataFilter: function (data) { return data; },
                      success: function (data) {
                          response($.map(data.d, function (item) {
                              // console.log(item);
                              return {
                                  value: item.ItemName,
                                  code: item.ItemCode,
                                  name: item.ItemName,
                                  price: item.ItemPrice,
                                  multiple: item.Multiple,
                                  vat: item.Vat,
                                  discount: item.Discount
                              }
  
                          }))
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          //alert(errorThrown);
                          return false;
                      }
                  });
              },
              minLength: 1,
              autoFocus: true,
              select: function (event, ui) {
  
                  $("#tbAutoItemCode" + index).val(ui.item.code);
                  $("#tbAutoItemName" + index).val(ui.item.name);
                  $("#tbAutoItemPrice" + index).val(numberToCurrency(ui.item.price));
                  $("#tbAutoItemMultiple" + index).val(ui.item.multiple);
  
                  var l_DiscPrcnt = Math.round(ui.item.discount * 100) / 100;
  
                  $("#discount" + index).val(l_DiscPrcnt);
  
                  //round the display of discount percent to two decimal places
                  var l_nAfterDiscount = (ui.item.price * ui.item.multiple) - ((ui.item.price * ui.item.multiple) * (ui.item.discount / 100));
                  l_nAfterDiscount = Math.round(l_nAfterDiscount * 100) / 100;
                  $("#tbAutoItemAfterDiscount" + index).val(l_nAfterDiscount);
                  $("#tbVatPer" + index).val(ui.item.vat);
                  $("#tbItemVat" + index).val(l_nAfterDiscount * ui.item.vat / 100);
                  $("#tbAutoItemComments" + index).click(function () {
                      openItemDescription(ui.item.code, ui.item.name)
                      return false;
                  });
  
                  reCalculateRow(index);
                  if(index == $('#orderTable tr:last').index()){
                      addRow();
                  }
                  return false;
              }
          });
  
      } // if length &gt; 0
  }); //keyup
  
  
  
   $('.inputItemName').live("focusout", function () {
  
  
      var index = $(this)[0].attributes["indexer"].value;
      var text = $(this).val().trim();
      if (text.length == 0) { return };
      setTimeout(function(){
      //console.log("Lost focus from indexer " + indexer + " with value " + $(this).val());
      $.ajax({
          url: "Services/FacAjax.asmx/getCompletionItemList",
          data: "{ 'prefixText': '" + text + "','count': '1','mode':'name'}", //search for name!
          dataType: "json",
          type: "POST",
          async: false,
          contentType: "application/json; charset=utf-8",
          dataFilter: function (data) { return data; },
          success: function (dataArr) {
  
              if (dataArr.d.length == 0) {
                  $("#tbAutoItemName" + index).val("");
                  reCalculateRow(index);
                  return;
              }
              var data = dataArr.d[0];
  
              $("#tbAutoItemCode" + index).val(data.ItemCode);
              $("#tbAutoItemName" + index).val(data.ItemName);
              $("#tbAutoItemPrice" + index).val(numberToCurrency(data.ItemPrice));
              $("#tbAutoItemMultiple" + index).val(data.Multiple);
  
              var l_DiscPrcnt = Math.round(data.Discount * 100) / 100;
  
              $("#tbAutoItemDiscountPercent" + index).val(l_DiscPrcnt);
  
              //round the display of discount percent to two decimal places
              var l_nAfterDiscount = (data.ItemPrice * data.Multiple) - ((data.ItemPrice * data.Multiple) * (data.Discount / 100));
              l_nAfterDiscount = Math.round(l_nAfterDiscount * 100) / 100;
              $("#tbAutoItemAfterDiscount" + index).val(l_nAfterDiscount);
              $("#tbVatPer" + index).val(data.Vat);
              $("#tbItemVat" + index).val(l_nAfterDiscount * data.Vat / 100);
              $("#tbAutoItemComments" + index).click(function () {
                  openItemDescription(data.ItemCode, data.ItemName)
                  return false;
              });
  
              reCalculateRow(index);
              //focusItemNameColumn(index);
              //focusQtyColumn(index);
              $(".ui-autocomplete").hide();
              return false;
          }
  
      }) }, 500);
  }); //focusout
  
  
  // for bar-code scanner
  $(".inputItemNum").live('click', function () {
      $(this).select();
  });
  
  $(".inputItemName").live('click', function () {
      $(this).select();
  });
  </code></pre>