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();
jQuery val() Method The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.
val()Returns: String or Number or Array. Description: Get the current value of the first element in the set of matched elements.
There are two usage of jQuery val() method. It is used to get current value of the first element in the set of matched elements. It is used to set the value of every matched element.
attr('value') returns the value that was before editing input field. And . val() returns the current value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With