Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get the value from selectbox by its text [duplicate]

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?

like image 731
nixon1333 Avatar asked Jul 06 '13 11:07

nixon1333


2 Answers

Using filter method (exact match):

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

Using :contains selector:

$('select option:contains(4 Years)').val();
like image 159
undefined Avatar answered Nov 17 '22 20:11

undefined


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

$('select option:selected').text();
like image 6
Falguni Panchal Avatar answered Nov 17 '22 20:11

Falguni Panchal