I am trying to dynamically load cities into the select tag in HTML. I am able to choose the state and then i want to load the city through an ajax call.
As far as console-logging the returned data it works, I am stuck with how i load this into the select option tags. Please help.
<select name="state" class="state">
<option value="" selected disabled>Choose State</option>
<?php foreach ($states as $state) {
echo "<option value='".$state->id."'>$state->name";
}?>
</select>
<select name="city" class="city" id="city">
<option value="" selected disabled>Choose City</option>
</select>
$.ajax({
type: "POST",
url: "includes/get_city.php",
data: {
state: selectedState
},
}).done(function(data) {
console.log(data); // Works when I console log
//Don 't know if this is the way
$('#city' > option).each(data, function(index, value) {
});
}