JQuery examples

From HaFrWiki
Jump to: navigation, search

Combobox[edit]

Selected text[edit]

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:

  1. The most simple solution (and working solution), but imho very difficult to remember.
    $("#yourdropdownid option:selected").text();
  2. A more optimized way, but not working for me.
    $("#yourdropdownid option").is("selected").text()
  3. One stated this is the fastest. But imho not working anymore!
    $("#yourdropdownid").children("option").is("selected").text()
  4. 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[edit]

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[edit]

See also[edit]

top

Reference[edit]

top

  1. Stack Overflow, Get selected text from dropdownlist using jquery