Okay, I have this code:
<select name="selector" id="selector">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
And I’d like to get the value of the option selected.
Example:
‘Option 2’ is selected, and the value of it is ‘2’. The ‘2’ is the value I need to get and not ‘Option 2’.
04/2020: Corrected old answer
Use :selected pseudo selector on the selected options and then use the .val function to get the value of the option.
Side note: Using filter is better then using
:selectedselector directly in the first query.If inside a change handler, you could use simply
this.valueto get the selected option value. See demo for more options.Old answer:
Edit: As many pointed out, this does not returns the selected option value.
~~Use .val to get the value of the selected option. See below,