Susitha
Susitha

Reputation: 3349

jquery cant set selected option dynamically created drop down list

I have populated a set of options from web service in a mobile app.I wanted to set selected for returning users, But cant set selected as a option in dropdown list.

HTML

<select id="select_group_drop">
       <option >[Select]</option>
</select>

Javascript

 function get_teams(){

 var servicePath = "http://domain.com/server/";
 function getGroups()
 {
    $("#select_group_drop").html("");
    $.ajax({
          url: servicePath + "manage_teams.php",
          data:
            {
                action: "getAllTeams",
                email: localStorage.getItem("email")
            },
    dataType: "json",
    method: "post",
    success: function(data)
    {
        $("#select_group_drop").html(data.results);
    },
    complete: function() {
        var selectedGroup = localStorage.getItem('selectedGroupName');

       $("#select_group_drop option[text="+selectedGroup+"]").attr("selected","selected");

    }


});

}

Upvotes: 0

Views: 855

Answers (1)

Mahbubur Rahman Manik
Mahbubur Rahman Manik

Reputation: 5161

If select options generate correctly then it should work:

   complete: function() {
    var selectedGroup = localStorage.getItem('selectedGroupName');
    $('#select_group_drop option').each(function(){              
             if($(this).val()==selectedGroup ){
                $(this).attr('selected','selected');
            }
        });
    }

Upvotes: 1

Related Questions