mrmarkfrench / country-select-js

A quick jQuery-based country picker based on https://github.com/Bluefieldscom/intl-tel-input.git
MIT License
282 stars 107 forks source link

How to get the iso2 code in the name instead of the name field? #85

Open valtu4a opened 3 years ago

valtu4a commented 3 years ago

In the example from the plugin docs I have found additional attribute data-countrycodeinput="1" There is also a part of the code which makes a hidden input if it doesn't exist but it doesn't work for me.


// Add the hidden input for the country code
            this.countryCodeInput = $("#"+this.countryInput.attr("id")+"_code");
            if (!this.countryCodeInput) {
                this.countryCodeInput = $('<input type="hidden" id="'+this.countryInput.attr("id")+'_code" name="'+this.countryInput.attr("name")+'_code" value="" />');
                this.countryCodeInput.insertAfter(this.countryInput);
            }
mrmarkfrench commented 3 years ago

Interesting. In general, our instructions are that if you want to have the ISO code, you should add the field yourself. I am surprised to see that the code appears to attempt to add it for you.

I'll look into it. In the meantime, just add the field yourself if you want the code.

boadusamuel commented 1 year ago

@mrmarkfrench I was trying to set the country dynamically with using the country name from the backend but it was not working as expected. I had to change the country name to iso2 before being able to achieve that. I added $.fn[pluginName].getIso2FromCountry = function(country) { return countries.find(c => c.name === country)?.iso2 || null; } to get the iso2 before passing it as the defaultCountry. Please let me know your thought if there is already a solution otherwise I make a PR