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'.
One of my options didn't have a value:
<option>-----</option>
. I was trying to useif (!$(this).val()) { .. }
but I was getting strange results. Turns out if you don't have avalue
attribute on your<option>
element, it returns the text inside of it instead of an empty string. If you don't wantval()
to return anything for your option, make sure to addvalue=""
.You need to add an id to your select. Then:
$('#selectorID').val()
OR
OR
Look at the example:
Try this:
or
this code work very well for me: this return the value of selected option. $('#select_id').find('option:selected').val();