Skip to content Skip to sidebar Skip to footer

JQuery - Select Val() Only Returns First Value Of Dropdown

I have a simple form summary script that returns all values entered in a form, and displays the values in a concise format in a dialog window. The script works as intended for inpu

Solution 1:

You have alot of code if you just want to get the selected fruit value:

HTML:

<p><label> <b>*</b> Fruit </label>
  <select name="fruit" id="fruit">
    <option value="A">Apple</option>
    <option value="B">Orange</option>
    <option value="C">Pear</option>
  </select>
</p>

<div id="dialogsummary"></div>

JavaScript:

$("#fruit").change(function() {
    $("#dialogsummary").text("You selected: " + $(this).val());
});

jsFiddle

The clue is basically: $(".fruit").val() to get the selected value.
If you want instead the text (since you're building a summary): $(".fruit").find(":selected").text();

Update
If you want something 'generic' instead: (not tested)

var str = "";
summary.find("select").each(function() {
    str += "Value for " + $(this).prop('name') + " is " + $(this).val();
});

Post a Comment for "JQuery - Select Val() Only Returns First Value Of Dropdown"