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?

2

3 Answers 3

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(); 
Sign up to request clarification or add additional context in comments.

Comments

6

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

$('select option:selected').text(); 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.