$(document).ready(function () {
var city = $('#city');
var town = $('#town');
$('#country').change(function () {
var country = $(this).val();
if (country != 'Türkiye') {
$('#city').replaceWith('<input class="form-control" type="text" name="city" id="city">');
$('#town').replaceWith('<input class="form-control" type="text" name="town" id="town">');
} else {
$('#city').replaceWith(city);
$('#town').replaceWith(town);
}
});
// Need to attach event handlers for newly inserted city options
$(document).on('change','#city' ,function(){
var city = $(this).val();
requestTownList(city);
});
// Handle town list acquired from AJAX
var handleTownList = function(townList){
// Clear town options
$("#town").html("");
// Then append town names based on selected city
for(var i=0; i<townList.length; i++){
$("#town").append("<option value='"+townList[i].id
+"' >"+townList[i].text+"</option>");
}
};
// Lines below are mocking AJAX request-response
var requestTownList = function(cityId){
// Define result that will be returned by AJAX response
var townList;
if(cityId == "Izmir"){
townList = [
{id: 'bergama',text:"Bergama"},
{id: 'bornova',text:"Bornova"},
{id: 'buca',text:"Buca"}];
}else if( cityId= "Ankhara" ){
townList = [
{id: 'bala',text:"Bala"},
{id: 'evren',text:"Evren"},
{id: 'mamak',text:"Mamak"}];
};
// Mock AJAX request
$.ajax({
url: '/echo/json/',
type: 'POST',
data: "val=myvalue",
success: function(d){
// FIXME: list of town should be acquired from server.
// Will be delay, caused by jsfiddle's AJAX mocking.
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!