/* --------------------- GET STATES FROM COUNTRY ID ------------------------ */ jQuery(function ($) { //console.log("test"); $("select.country_auto").change(function () { var form = (this).closest("form"); //console.log(form); if ($(this).closest("form").find("select.state_auto").length > 0) { var cnt = $(form).find("select.country_auto").children("option:selected").attr('data-id'); //console.log(cnt); $(form).find("select.state_auto").html(''); $(form).find("select.city_auto").html(''); if (cnt == '0') { $(form).find("select.state_auto").html(''); return false; } jQuery.ajax({ url: tc_csca_auto_ajax.ajax_url, type: 'post', dataType: "json", data: { action: "tc_csca_get_states", nonce_ajax: tc_csca_auto_ajax.nonce, cnt: cnt }, success: function (response) { if (response.length > 0) { $(form).find("select.state_auto").html(''); for (i = 0; i < response.length; i++) { var st_id = response[i]['id']; var st_name = response[i]['name']; var opt = ""; $(form).find("select.state_auto").append(opt); } } else { $(form).find("select.state_auto").html(''); var opt = ""; $(form).find("select.state_auto").append(opt); console.log("State List Not Found"); } } }); } }); /* --------------------- GET CITIES ------------------------ */ $("select.state_auto").change(function () { var form = (this).closest("form"); //console.log(form); if ($(this).closest("form").find("select.city_auto").length > 0) { var sid = $(form).find("select.state_auto").children("option:selected").attr('data-id'); //console.log(sid); $(form).find("select.city_auto").html(''); if (sid == '0') { $(form).find("select.city_auto").html(''); return false; //return rv; } jQuery.ajax({ url: tc_csca_auto_ajax.ajax_url, type: 'post', dataType: "json", data: { action: "tc_csca_get_cities", nonce_ajax: tc_csca_auto_ajax.nonce, sid: sid }, success: function (response) { if (response.length > 0) { $(form).find("select.city_auto").html(''); for (i = 0; i < response.length; i++) { var ct_id = response[i]['id']; var ct_name = response[i]['name']; var opt = ""; $(form).find("select.city_auto").append(opt); } } else { $(form).find("select.city_auto").html(''); var opt = ""; $(form).find("select.city_auto").append(opt); console.log("City List Not Found"); } } }); } }); });