JQuery examples: Difference between revisions
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
== Combobox == | == Combobox == | ||
=== Selected text === | === Selected text === | ||
Q: How can I get a dropdownlist/combobox selected text in jQuery? | Q: How can I get a dropdownlist/combobox selected text in jQuery? <ref>[http://stackoverflow.com/questions/1643227/get-selected-text-from-dropdownlist-using-jquery Stack Overflow], Get selected text from dropdownlist using jquery</ref>. | ||
<br>A: There are several possibilities which can be found on the Internet: | <br>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.<pre>$("#yourdropdownid option:selected").text();</pre> | # The most simple solution (and working solution), but imho very difficult to remember.<pre>$("#yourdropdownid option:selected").text();</pre> |
Revision as of 11:05, 30 January 2013
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();
See also
Reference
- ↑ Stack Overflow, Get selected text from dropdownlist using jquery