How can I change the selected text of a DropDownList using JQuery?

I have a dropdown with id = "ddlTitle" .

It contains three values ​​as stated below
Mr.
Ms
Miss

The currently selected item is Mr. I want to change the selected text to a different dropdown text on page refresh.

I am using $ ('#' + ddlTitle + ': selected'). text (Title) ; where Title can be Mr. or Mrs. or miss

There is one problem: if I use the above code, I can change the selected text, but whatever was previously selected text was not showing up in the DropDownList.

Example: If the currently selected text is a verb then after using this code $ ('#' + ddlTitle + ': selected'). Text ('Miss'); The currently selected text becomes Miss, but the values ​​in the dropdown list become as shown below:

Miss
Mrs
Miss

Where the previous values ​​were

Mr.
Ms
Miss

+3


source to share


1 answer


You have an answer in this thread:

How can I select an element by its text value in a dropdown menu using jQuery?



you can use this code for example:

$("#ddlTitle option").each(function() {
  this.selected = $(this).text() == Title;
});

      

+4


source







All Articles