3

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

Gaurav123
  • 5,059
  • 6
  • 51
  • 81

1 Answers1

4

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;
});
Community
  • 1
  • 1
freshbm
  • 5,540
  • 5
  • 46
  • 75