8

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?

Jason
  • 15,017
  • 23
  • 85
  • 116
nixon1333
  • 531
  • 1
  • 9
  • 23

3 Answers3

8

Using filter method (exact match):

$('select option').filter(function(){
    return $(this).text() === '4 Years';
}).val();

Using :contains selector:

$('select option:contains(4 Years)').val();
Ram
  • 143,282
  • 16
  • 168
  • 197
6

Try using .text() on the selected option. e.g.:

$('select option:selected').text();
Falguni Panchal
  • 8,873
  • 3
  • 27
  • 33
5

If you want the value of the selected option:

$('#year').val();

If you want a specific option by its text:

$("#year option:contains('4 Years')").val();

Regards.

ezakto
  • 3,174
  • 22
  • 27