If I have this select:
<select id="days">
<option value="0">Today</option>
<option value="1">Yesterday</option>
<option value="7">Last week</option>
</select>
and someone selects the 3rd option 'last week', I can get the value of last week (which is 7), using $("#days").val()
, but how can I get the value of the text i.e 'Last week'?
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
$("#days option:selected").text()
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