JQuery Autoselect: I want to run a function always on select event, but on change event only if value is not selected

173 Views Asked by At

I am using jQuery autocomplete on a text input and want an immediate response if the user clicks one of the autocomplete options. I also want to respond to a change event if the user manually typed something or modified what they selected from the autocomplete.

In short, I don't want to redundantly run the function if input text is the same between select and change events.

  function attachPartAutocomplete(id, changeFunc) {
    $("#" + id).autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "AJAXHandlerInLogisitcs",
                dataType: "json",
                data: {
                    method_name: "searchPart",
                    rows: 20,
                    part: request.term,
                    task: "com.acme.logistics.ReqAjax"
                },
                success: function(data) {
                    response(data);
                }
            });
        },
        select: function(event, ui) {
            if (typeof changeFunc == 'function') {
                changeFunc(ui.item.value);
            }
        },
        change: function(event, ui) {
            if (typeof changeFunc == 'function') {
                changeFunc(this.value);
            }
        },
        delay: 0,
        minLength: 2,
        open: function() {
            $(".ui-autocomplete").css({
                fontSize: "75%",
                overflowY: "auto"
            });
        }
    });
}

Is there an event after select that I can set a flag if something happens? Is there a way to get rid of the pending change event on select event? That way I would only get a change event if something happened after select. What is an elegant way to handle this?

0

There are 0 best solutions below