How to manually set a value (and fire select event) in jQuery autocomplete

I know there are several questions about this, but I couldn't find anyone that sets the value and also triggers the select function.

My code:

$("#ux-selfservice-account-edit-nationality").autocomplete({
    source: countryList,
    minLength: 1,
    autoFocus: true,
    select: function(event, ui) {
        $(this).val(ui.item.label).attr("oid", ui.item.oid);

        var select = $(this).closest(".ux-selfservice-account-box-edit").find(".ux-selfservice-account-edit-dni-type");
        // Check if the user has selected a different country (against the site)
        if (ui.item.iataCode == options.countryCode) {
            $(select).find("option").show();
        }
        else {
            $(select).find("option:not([value='PAS']):not([value=''])").hide();
            if ($(select).val() != "PAS") $(select).val('');
        }
        return false;
    },
    focus: function(event, ui) {
        return false;
    },
    search: function(event, ui) {
        $(this).attr("oid", "0");
    }
});

      

The list of countries looks like this:

[
    {
        iataCode: "AR",
        label: "Argentina",
        value: "Argentina",
        oid: 28515
    },
    ....
]

      

As you can see I have very little validation in the select function, if the user selects a different country, I hide or show some options from the other dropdown.

Now my problem is that sometimes I want the country to be configured with javascript so that the user sees the country name in the field, the oid is set to attr and also checks the country.

Now I am doing something like this.

$("#ux-selfservice-account-edit-nationality").val(getCountryName(profile.personalInfo.country)).attr("oid", profile.personalInfo.country);

      

But of course this is wrong and does not test another check. also i can't do validation here because i don't have countryCode (iataCode). I know I can find it in the list, but need to use the same autocomplete feature.

+3


source to share


1 answer


Why don't you extract the logic in your select event handler for a separate function that you can call elsewhere from your Javascript code?

All you need in this function is the country input (which is this

in your current code, and the selected country is json, which is ui.item

);

So, you can extract your current logic into a new function countrySelected

:

var countrySelected = function (countryInput, countryJSON) {
    $(countryInput).val(countryJSON.label).attr("oid", countryJSON.oid);

    var select = $(countryInput).closest(".ux-selfservice-account-box-edit").find(".ux-selfservice-account-edit-dni-type");
    // Check if the user has selected a different country (against the site)
    if (countryJSON.iataCode == options.countryCode) {
        $(select).find("option").show();
    } else {
        $(select).find("option:not([value='PAS']):not([value=''])").hide();
        if ($(select).val() != "PAS") $(select).val('');
    }
}

      

Then update your autocomplete declaration so that the select event handler uses this function:



$("#country").autocomplete({
    minLength: 1,
    source: countries,
    autoFocus: true,
    select: function (event, ui) {
        //just call the new function
        countrySelected(this, ui.item);
        return false;
    },
    focus: function (event, ui) {
        return false;
    },
    search: function (event, ui) {
        $(this).attr("oid", "0");
    }
});

      

This way you can also manually call the function countrySelected

:

var countries = [{
    iataCode: "AR",
    label: "Argentina",
    value: "Argentina",
    oid: 28515
},
...
];
countrySelected($("#country"), countries[0]);

      

I created this script where you can see it in action.

+2


source







All Articles