Difference between revisions of "JQuery examples"

From HaFrWiki
Jump to: navigation, search
(Created page with "{{TOCright}} == Combobox == === Selected text === Q: How can I get a dropdownlist/combobox selected text in jQuery? <br>A: There are several possibilities: # The most simple ...")
 
m (Selected text)
Line 4: Line 4:
 
=== 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?
<br>A: There are several possibilities:
+
<br>A: There are several possibilities which can be found on the Internet:
# The most simple 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>
# A more optimized way. <pre>$("#yourdropdownid option").is("selected").text()</pre>
+
# A more optimized way, but not working for me. <pre>$("#yourdropdownid option").is("selected").text()</pre>
# One stated this is the fastest.<pre>$("#yourdropdownid").children("option").is("selected").text()</pre>
+
# One stated this is the fastest. But imho not working anymore!<pre>$("#yourdropdownid").children("option").is("selected").text()</pre>
# One stated ''is()'' should not be used because it is a boolean whether selected or not.<pre>$("#yourdropdownid").children("option").filter(":selected").text()</pre>
+
# One stated ''is()'' should not be used because it is a boolean whether selected or not. Same problem does not work anymore.<pre>$("#yourdropdownid").children("option").filter(":selected").text()</pre>
 +
 
 +
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: <pre>$("#yourdropdownid option:selected").val();</pre>
 +
* To get the selected text-value of the combo box: <pre>$("#yourdropdownid option:selected").text();</pre>
 +
 
 +
== List box ==
 +
A list box seems to be somewhat more complicated. For me the following construction works fine:
 +
<pre>var cList = $("input[name=yourlistname]");
 +
key = cList.filter(":checked").val();
 +
text = cList.filter(":checked").text();
 +
</pre>
  
 
== See also ==
 
== See also ==

Revision as of 11:03, 30 January 2013

Combobox

Selected text

Q: How can I get a dropdownlist/combobox selected text in jQuery?
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

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

top

Reference

top