country-regions / country-region-selector

Dynamic country and region dropdowns for your forms.
http://country-regions.github.io/country-region-selector/
MIT License
255 stars 115 forks source link

Solution : window.crs.populateRegionFields(countryElement, regionElement) **FOR "RELOADING" THE REGION SELECT** #57

Open CreativeHouseDotOrg opened 7 years ago

CreativeHouseDotOrg commented 7 years ago

Hello

I added 1 line of code in your plugin today and i wanted to "commit" my change here:

File: jquery.crs.js (0.4.0)

Line 212: (Added) populateRegionFields: _populateRegionFields

So I can call this specific row of code to update the Region Select after I'm setting the value via jQuery:

EXAMPLE window.crs.init(); $.getJSON('//freegeoip.net/json/', function(result) { $('#crs-country').val(result.country_code); window.crs.populateRegionFields($('#crs-country')[0],$('#crs-region')[0]); });

This Method will locate the current CountryCode from freegeoip.net and determine from which Country the Visitor is coming from and it will assign the value automatically to the Country Select and "repopulate" the Region Select with the current Country Select Value.

I hope I could help a little for others that run in to the same "issue" of a missing region reload method C:

Kind Regards

Mike of Creative-House.org

wobblymusic commented 7 years ago

This was really helpful. Two comments:

1) You need to put a comma at the end of Line 211, i.e. return { init: _init, populateRegionFields: _populateRegionFields };

2) While this does populate the regions field with the correct drop down list, it does not make the data-default-value the selected option. For that, you'll need to add a line like $("#crs-region").val($("#crs-region").attr("data-default-value"));

wobblymusic commented 7 years ago

Incidentally, there should be a way to write a method that does the same thing, but doesn't require any arguments. I'm not enough of a javascript wiz to figure it out. Thankfully, your simple change does the trick, albeit less elegantly.