Auto Select Dynamic Dropdown Matching value of php variable

i want to automatically select value of dropdown matching value of php variable

here is my

    <select class="destinationId" id="destination" name="destinationId" onchange="getNationalityAndLiving(this.value,'','');">
                  <option value="" disabled selected>Select Traveling to</option>
                                <option data-prefix="azerbaijan" value="azerbaijan" >
                  Azerbaijan              </option>
                                <option data-prefix="cambodia" value="cambodia" >
                  Cambodia              </option>
                                <option data-prefix="egypt" value="egypt" >
                  Egypt              </option>
                                <option data-prefix="india" value="india" >
                  India              </option>
                                <option data-prefix="kenya" value="kenya" >
                  Kenya              </option>
                              </select>

<select class=“nationality” name=“nationality” id=“nationality”>
<option value=“” disabled selected>Select Citizen of</option>
</select>

this is my ajax code to retrive dynamic dropdown for select select option “nationality”

    <script>
$(document).ready(function(){

$("#destination").change(function(){
    var deptid = $(this).val();
    var dbnm = "country";
    var res = dbnm.concat(deptid);
    $('.overlay').show();
    $.ajax({
        url: 'getcountry.php',
        type: 'post',
        data: {dest:res},
        dataType: 'json',
        success:function(response){
            var len = response.length;

            $("#nationality").empty();
            for( var i = 0; i&lt;len; i++){
                var id = response[i]['id'];
                var name = response[i]['name'];

                $("#nationality").append("&lt;option data-nation='"+name+"' value='"+name+"'&gt;"+name+"&lt;/option&gt;");

            }
           $('.overlay').hide();            
        }
    });
});

});
</script>

i have some dynamic php variable coming on this page from previous url. for example

$nationality = “some value”;

What i want is selected dropdown with value of $nationality in second dropdown,

if it was static i could have achieved this, but how to auto select in dynamic i am not able to get any workaround.

#javascript #php #jquery

2 Likes14.50 GEEK