I'm working on a component in Jooomla 3, and I want to stick with the system version of Bootstrap to keep it as integrated looking as possible... so I'm not looking for answers saying to upgrade to BS 3 or 4. This also isn't directly related to Joomla, so I'm not posting it over there.
The select drop-down for country has a change event populate the available states. Technically it does work, but the state drop down doesn't actually display correctly, I can only see the browser console elements but the list doesn't show up visually. If I do this by using a div ID and create the whole select element then it works, but I feel like I'm just missing something in the way I should handle the populating.
Here's my JQuery:
$("select#registrant_country").change(function () {
var selectedCountry = $("#registrant_country option:selected").val();
$('#registrant_state').empty();
$.getJSON("index.php?option=com_mycomponent&task=tools.countryState",
{id: selectedCountry,
tmpl: "component"
})
.done(function (data) {
$.each(data, (val) => {
$("#registrant_state").append("<option value='" + val +"'>" + val + "</option>");
});
});
});
Here's the PHP tools.countryState method:
public function countryState() {
JFactory::getDocument()->setMimeEncoding('application/json');
JResponse::setHeader('Content-Disposition', 'attachment;filename="state-results.json"');
$app = JFactory::getApplication();
$country = $app->input->get('id', '', 'string');
MysiteHelper::add('country-code', $country); //adds "country-code=US" to the query
$result = MysiteHelper::getData('country/state-list.json'); // becomes https://API_PATH/country/state-list.json&country=US
echo json_encode($result);
jexit();
}
And I have a simple select tag to append the results:
<label>State</label><select id="registrant_state"></select>
From the console, before change:
<select id="registrant_state" style="display: none;"></select><div class="chzn-container chzn-container-single chzn-container-single-nosearch" style="width: 220px;" title="" id="registrant_state_chzn"><a class="chzn-single chzn-default"><span>Select an option</span><div><b></b></div></a><div class="chzn-drop"><div class="chzn-search"><input type="text" autocomplete="off" readonly=""></div><ul class="chzn-results"></ul></div></div>
From the console after a change (note the Bootstrap set style="display: none;"):
<label>State</label>
<select id="registrant_state" style="display: none;">
<option value="Navassa Island">Navassa Island</option>
<option value="Howland Island">Howland Island</option>
<option value="Kingman Reef">Kingman Reef</option>
<option value="Jarvis Island">Jarvis Island</option>
<option value="Johnston Atoll">Johnston Atoll</option>
<option value="Midway Islands">Midway Islands</option>
<option value="Wake Ialand">Wake Ialand</option>
<option value="Palmyra Atoll">Palmyra Atoll</option>
<option value="Baker Island">Baker Island</option></select>
<div class="chzn-container chzn-container-single chzn-container-single-nosearch" style="width: 220px;" title="" id="registrant_state_chzn">
<a class="chzn-single chzn-default"><span>Select an option</span><div><b></b></div></a>
<div class="chzn-drop">
<div class="chzn-search"><input type="text" autocomplete="off" readonly=""></div>
<ul class="chzn-results"></ul>
</div>
</div>