JQuery examples: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to 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 ..."
 
 
(4 intermediate revisions by the same user not shown)
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:
<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>
== jQuery ui ==
* [http://code.tutsplus.com/tutorials/create-a-dynamic-content-editing-system-with-jquery-ui--pre-13149 Code tut plus], Create a dynamic editing system using jQuery ui.
== See also ==
== See also ==
<span class="editsection">[[#content|top]]</span>
<span class="editsection">[[#content|top]]</span>
* [http://www.jeasyui.com/index.php jQuery EasyUI], jQuery EasyUI framework.


== Reference ==
== Reference ==
Line 18: Line 32:


[[Category:Index]]
[[Category:Index]]
[[Category:Java]]
[[Category:programming]]
[[Category:Tools]]

Latest revision as of 10:07, 1 September 2016

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:

  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();

jQuery ui

See also

top

Reference

top

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