10

Good Morning,

I have the following code:

$("#close-request-field-clinic").autocomplete({
                source: arrayClinic,
                delay: 0,
                minLength: 0,
                isDivider: function( item ) {
                  return false;
                },
                focus: function ( event, ui ) {
                    $('#close-request-field-clinic').val( ui.item.label );
                    return false;
                },
                select: function( event, ui ) { 
                    $('#close-request-field-clinic').val( ui.item.label );
                    if(ui.item.value == -1) {
                        resetField('#close-request-field-clinic', false);
                    } else {
                        successField('#close-request-field-clinic');
                        setKey(finalValues, 'clinic', ui.item.value);
                        if(msieversion()) {
                            $(this).blur();
                        }
                    }
                    checkValidation(fieldCheck,'#close-request-personal-information-next');
                    return false;
                },
                change: function( event, ui ) {
                    alert('change');
                    if(!ui.item) {
                        resetField('#close-request-field-clinic', false);
                        removeKey(finalValues, 'clinic');
                    }
                    checkValidation(fieldCheck,'#close-request-personal-information-next');
                    return false;
                }
            }).focus(function(){$(this).autocomplete("search", "")});

The majority of this works perfectly, however, it seems that when the field is changed the 'change' event is not always triggered.

When selecting an element from the list, it works fine, however if you delete the values in the box using the backspace (delete) key, and then click off the text box, the change event is only sometimes called. Is there something I'm missing?.. It seems to only not be called when values are deleted manually.

Regards, Josh

Joshua
  • 588
  • 1
  • 5
  • 19
  • possible duplicate of [jQuery AutoComplete Trigger Change Event](http://stackoverflow.com/questions/6431459/jquery-autocomplete-trigger-change-event) – allicarn Sep 18 '15 at 17:57

1 Answers1

10

This is alternative clean solution

$("#close-request-field-clinic").on("autocompletechange", function(event,ui) {
       alert($(this).val());
    });

This answer copied from here

Community
  • 1
  • 1
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22
  • 1
    Is there an explanation of why on('autocompletechange') works, while the change event of the autocomplete widget doesn't work as expected? – osullic Dec 22 '16 at 15:56
  • I am not 100% sure but i found it was not working for me because of jquery older version – Faruk Omar Dec 22 '16 at 16:32
  • I would use **.on("autocompletechange keyup")** considering that the change event is not fired if you don't blur out of the field. – Andrea Mauro Jan 31 '20 at 12:52