Wil Wells
Wil Wells

Reputation: 205

jQuery .val() is returning .text() not .val()

I have this code

<select id="month">
    <option val='0'>January</option>
    <option val='1'>February</option>
</select>

I'm using this to try to get the values:

$month = $("#month option:selected").val();

But it's returning the text "January" and "February" not "0" or "1"

I tried this and got the same results:

$month = $("#month").val();

Upvotes: 9

Views: 3004

Answers (1)

Barmar
Barmar

Reputation: 780663

The attribute should be value, not val:

<option value='0'>January</option>
<option value='1'>February</option>

When an option doesn't have a value attribute, the text is used as the default.

Upvotes: 29

Related Questions