Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery autocomplete else if item is not selected

Tags:

jquery

I am using this jquery autocomplete code:

$(document).ready(function(){
          var data = <?php echo $data; ?>;
          $("#contact_name").autocomplete({
              source:data,
              select: function(e, ui) {
                  e.preventDefault() // <--- Prevent the value from being inserted.
                  $("#contact_email").val(ui.item.value);
                  $(this).val(ui.item.label);

                  $("#AddAsContact").css('display', 'none');
                  $('#AddAsContact').prop('checked', false);
              }
          });
      });

its working great, exactly as it should but if an item from the auto complete list is not selected and something else is just typed into the text input, how can i run some different code. Kind of like else { ...code here... }

like image 995
user3843997 Avatar asked Dec 19 '14 08:12

user3843997


1 Answers

select event is triggered when you select something from the DropDown.

You can use the change event for determining whether the item falls under the autocomplete list or not,

 change: function (event, ui) {
         if (ui.item === null) {
              $(this).val('');
              $('#field_id').val('');
         }
       }

And, here is a Demo Fiddle for you.

Hope this help you.

like image 120
Runcorn Avatar answered Oct 29 '22 22:10

Runcorn