I have a select box like this:
<select id="year">
<option value="1">1 Year</option>
<option value="2">2 Years</option>
<option value="3">3 Years</option>
<option value="4">4 Years</option>
<option value="5">5 Years</option>
<option value="6">6 Years</option>
<option value="7">7 Years</option>
<option value="8">8 Years</option>
<option value="9">9 Years</option>
<option value="10">10 Years</option>
</select>
I want to get value of a specific text in jQuery, like "value of 4 Years". How can I do that?
Using filter
method (exact match):
$('select option').filter(function(){
return $(this).text() === '4 Years';
}).val();
Using :contains
selector:
$('select option:contains(4 Years)').val();
Try using .text() on the selected option. e.g.:
$('select 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