jQuery auto complete, change event not working as expected
Asked Answered
E

1

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

Eyde answered 27/1, 2015 at 11:2 Comment(1)
possible duplicate of jQuery AutoComplete Trigger Change EventGerik
A
10

This is alternative clean solution

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

This answer copied from here

Argentiferous answered 27/1, 2015 at 11:21 Comment(3)
Is there an explanation of why on('autocompletechange') works, while the change event of the autocomplete widget doesn't work as expected?Encomiast
I am not 100% sure but i found it was not working for me because of jquery older versionArgentiferous
I would use .on("autocompletechange keyup") considering that the change event is not fired if you don't blur out of the field.Tso

© 2022 - 2024 — McMap. All rights reserved.