Sandip Mandlecha
Sandip Mandlecha

Reputation: 93

Changing the Selected value of Select Menu Jquery Mobile

I have created a form and have stored the values selected by the user in a database. But now if the user wants to edit his form, i need to reload the form with the previous values.I am using JQUERY MOBILE

I retrieved his previous values from the database but now i am having a problem with loading the values in a select Menu.Can anyone help me ?

I have used the following code :-

     var nameVar = (dataset.item(id)['name']);  // Getting the name from the database
     $('#StateName').val(nameVar);                   // StateName is the id  
     $('#StateName').selectmenu('refresh', true);    // Refreshing the Select Menu

But even after the execution of the above code nothing is reflected in the select Menu Is something Wrong in Code or am i missing something ?

Upvotes: 3

Views: 13107

Answers (1)

Phill Pafford
Phill Pafford

Reputation: 85368

Instead of value you need to append the option:

var nameVar = (dataset.item(id)['name']);
$('#StateName').append('<option value="'+nameVar+'" selected="selected">'+nameVar+'</option>');                     
$('#StateName').selectmenu('refresh', true);  

Or if you have the Select Options already and just need to select the option, try this:

var nameVar = (dataset.item(id)['name']);
$('#StateName option[value='+nameVar+']').attr('selected', 'selected');
$('#StateName').selectmenu('refresh', true);

More on Select Menu for jQM 1.0 here:

Upvotes: 10

Related Questions