Skip to content

Instantly share code, notes, and snippets.

@barankaynak
Created November 12, 2021 11:57
Show Gist options
  • Save barankaynak/b81eab88e8414accb12c27c7f314d1ae to your computer and use it in GitHub Desktop.
Save barankaynak/b81eab88e8414accb12c27c7f314d1ae to your computer and use it in GitHub Desktop.
jquery sample
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$("#selectSehir").change(function()
{
let selected = $(this).val();
$("div").children().last().text(selected);
});
$("#selectUlke").change(function () {
$("#selectSehir").empty().append($('<option>', {
value: "0",
text: "Şehir Seçiniz!"
}));
let selectedCountry = $(this).val();
$("div span:first").text(selectedCountry);
$.post("https://countriesnow.space/api/v0.1/countries/cities",
{
country: selectedCountry
},
function (serviceData, status) {
for (const iterator of serviceData.data) {
$("#selectSehir").append($('<option>', {
value: iterator,
text: iterator
}));
}
}
);
});
$.get("https://countriesnow.space/api/v0.1/countries/iso",
function (serviceData, status) {
for (const iterator of serviceData.data) {
$("#selectUlke").append($('<option>', {
value: iterator.name,
text: iterator.name
}));
}
});
}
);
</script>
</head>
<body>
<select id="selectUlke">
<option>Ülke Seçiniz</option>
</select>
<br>
<select id="selectSehir">
<option>Şehir Seçiniz</option>
</select>
<div>
Seçilen ülke:<span></span>
Seçilen Şehir:<span></span>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment