Dependent dropdowns (country, then city)

Load the second list when the first one changes.

jQuery · Dependent dropdowns (country, then city)Intermediate
$('#country').on('change', function () {
  const $city = $('#city').prop('disabled', true).html('<option>Loading…</option>');
  const code = $(this).val();
  if (!code) return $city.html('<option value="">Choose a country first</option>');

  $.getJSON('/api/cities', { country: code }, function (data) {
    $city.empty().append($('<option>', { value: '', text: 'Choose a city' }));
    $.each(data.items || [], function (i, name) {
      $city.append($('<option>', { value: name, text: name }));
    });
    $city.prop('disabled', false);
  });
});
selectajaxdependentoptions