Reputation: 5209
I have a dropdownlist having id="ddlTitle".
It contains three values as mentioned below
Mr.
Mrs.
Miss
Current selected item is Mr. I want to change the selected text to some other text of dropdownlist when page refresh.
I am using $('#' + ddlTitle + ' :selected').text(Title); where Title can be Mr. or Mrs. or Miss
There is one problem that if I use the above code, I am able to change the selected text but whatever was the selected text before, didn't appear in dropdownlist.
Example : If current selected text is Mr. then after using this code $('#' + ddlTitle + ' :selected').text('Miss'); Current selected text will become Miss but values in dropdownlist becomes as below:
Miss
Mrs.
Miss
Where as previous values were
Mr.
Mrs.
Miss
Upvotes: 3
Views: 10653
Reputation: 5622
You have answer in this thread:
How do I select an item by its text value in a dropdown using jQuery?
you can use this code for example:
$("#ddlTitle option").each(function() {
this.selected = $(this).text() == Title;
});
Upvotes: 4