Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an item by its text value in a dropdown using jQuery?

If I had the following select, and did not know the value to use to select an item in advance like in this question or the index of the item I wanted selected, how could I select one of the options with jQuery if I did know the text value like Option C?

<select id='list'>
<option value='45'>Option A</option>
<option value='23'>Option B</option>
<option value='17'>Option C</option>
</select>
like image 715
beckelmw Avatar asked Oct 27 '08 23:10

beckelmw


1 Answers

var option;
$('#list option').each(function() {
    if($(this).text() == 'Option C') {
        option = this;
        return false;
    }
});
like image 199
eyelidlessness Avatar answered Sep 16 '22 20:09

eyelidlessness