i am trying to remove and add new options for a select list after user choose a value from another select box. I'm using ajax to call for the api and everything works fine. However, the html of the select list doesn't refresh at all after i remove or add new options. Am i doing wrong somehow?
My HTML generated with handlebars:
<select name="district" id="district">
<option value="">Quận/ Huyện</option>
<option value="1">District 1</option>
<option value="2">District 2</option>
</select>
<select name="street-select" id="street">
<option value="">Đường</option>
{{#each streets}}
<option value="{{name}}">{{name}}</option>
{{/each}}
</select>
My javascript:
var dropdownStreet = $('#street');
var dropdownDistrict = $('#district');
$('#district').change(function () {
dropdownStreet.empty();
dropdownStreet.html('')
var value = $('#district').find(":selected").text();
$.ajax({
type: 'GET',
url: `http://localhost:3002/api/ajaxcall/getLocation`,
data: {
'value': value
},
success: function (data) {
console.log('success');
var result = data.data.streets
var streets = [];
streets = result.map(function (a) { return a.location; });
for (var i = 0; i < streets.length; i++) {
$('select[name=street-select]').append('<option value="' + streets[i] + '">' + streets[i] + '</option>').selectmenu('refresh',true);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error');
}
})
})
Thank you in advance.