Best way to implement Country-State Dropdown
What would be the best way to do the following:
1. A drop-down list with COUNTRYies. The list of countries are in json format, retrieved from a web service.
2. Show or pre-fill the STATE dropdown based on Country selection.
This looks to be a standard requirement but was not able to find a proper solution.
What we have done as of now if using Sightly, make a call to get the Country list in a json and populate, and based on selection show STATES(for USA) or PROVINCES(for Canada) and doing Show/Hide. But was looking for better alternatives.
<!-- COUNTRY --> <select class="myContactFieldSelect" id="companyCountry" tabindex="22" name="countryID"> <option value="">Please Select</option> <div data-sly-list.country="${jHelper.pJSON.countryList}" data-sly-unwrap> <option value="${jHelper.pJSON.countryList[country].countryID @ context='html'}"> ${jHelper.pJSON.countryList[country].countryLongName @ context='html'} </option> </div> </select> <!-- STATES or PROVINCES --> <select class="myContactFieldSelect" id="companyState" tabindex="20" name="stateLongName" style="display: none;"> <option selected="selected" value="">Please Select</option> <div data-sly-list.state="${jHelper.pJSON.stateListUS}" data-sly-unwrap> <option>${jHelper.pJSON.stateListUS[state].stateLongName @ context='html'}</option> </div> </select> <!-- OR --> <select class="myContactFieldSelect" id="companyProvince" tabindex="21" name="provinceLongName" style="display: none;"> <option selected="selected" value="">Please Select</option> <div data-sly-list.province="${jHelper.pJSON.stateListCA}" data-sly-unwrap> <option>${jHelper.pJSON.stateListCA[province].stateLongName @ context='html'}</option> </div> </select>