JQuery examples
Jump to navigation
Jump to search
Combobox
Selected text
Q: How can I get a dropdownlist/combobox selected text in jQuery? [1].
A: There are several possibilities which can be found on the Internet:
- The most simple solution (and working solution), but imho very difficult to remember.
$("#yourdropdownid option:selected").text();
- A more optimized way, but not working for me.
$("#yourdropdownid option").is("selected").text()
- One stated this is the fastest. But imho not working anymore!
$("#yourdropdownid").children("option").is("selected").text()
- One stated is() should not be used because it is a boolean whether selected or not. Same problem does not work anymore.
$("#yourdropdownid").children("option").filter(":selected").text()
So this leaves me to the conclusion the best way to get the values from a combobox are:
- To get the selected key-value of the combo box:
$("#yourdropdownid option:selected").val();
- To get the selected text-value of the combo box:
$("#yourdropdownid option:selected").text();
List box
A list box seems to be somewhat more complicated. For me the following construction works fine:
var cList = $("input[name=yourlistname]"); key = cList.filter(":checked").val(); text = cList.filter(":checked").text();
jQuery ui
- Code tut plus, Create a dynamic editing system using jQuery ui.
See also
- jQuery EasyUI, jQuery EasyUI framework.
Reference
- ↑ Stack Overflow, Get selected text from dropdownlist using jquery