Gaurav123
Gaurav123

Reputation: 5209

How to change selected text of DropDownList using Jquery?

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

Answers (1)

freshbm
freshbm

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

Related Questions