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
source to share
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;
});
source to share