Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  186] [ 4]  / answers: 1 / hits: 16708  / 12 Years ago, tue, july 24, 2012, 12:00:00

So I have the following piece of HTML:



<select id=sel>
<option value=0>Option1</option>
<option value=1>Option2</option>
<option value=2>Option3</option>
<option value=3>Option4</option>
</select>


How do I check to see if #sel has an option based on the text value? (Option1)


More From » jquery

 Answers
12

Try the following:



var opt = 'Option1';
if ($('#sel option:contains('+ opt +')').length) {
alert('This option exists')
}


Demo



edit: The above snippet uses the jQuery contains selector which filters elements that their textContent contains the specified value. For an exact match you can use the code snippet suggested in christian-mann's answer.




How to do this with Javascript (not jquery) – Jerry




var optionExists = [].some.call(document.getElementById('sel').options, function(option) {
return option.textContent === 'value';
});

[#84061] Monday, July 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
catrinas

Total Points: 587
Total Questions: 100
Total Answers: 105

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;