jquery - check if certain item is present in the select option case

253
February 04, 2017, at 3:00 PM

Suppose I have something like this

 <select name="mname" id="mid" size="15">
                    <option>TextA </option>
                    <option>TextB </option>
                    <option>TextC </option>
        </select>

How do I check for the presence of TextA in the list ? I know you can do like this

if($("select["value="TextA"]").length)
{
   //The item is present
}

However the above wont work as I dont have a value tag inside the select. How can I adjust the above for my case ?

I looked at the following question but none tell you how to deal with my case in which the value property is missing inside the option tag

Answer 1

Try using :contains() as shown :-

if($("#mid option:contains('TextA')").length)
{....}

DEMO

Answer 2

You can use :contains() Selector.

if($("select option:contains('TextA')").length){ 
   console.log("Item found"); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<select name="mname" id="mid" size="15"> 
  <option>TextA </option> 
  <option>TextB </option> 
  <option>TextC </option> 
</select>

Would you please check above snippet?

READ ALSO
how to fire a function from input tag when input is not typed into it ?

how to fire a function from input tag when input is not typed into it ?

I'm working with angular2 color pickerThe value of input element changes when color is selected from color picker and not typed

208
how to send variable values through url using javascript in google login

how to send variable values through url using javascript in google login

I am sending question title value through js ,but I am not getting the value in my classphp file

268
Uncaught TypeError: (intermediate value)(&hellip;) is not a function

Uncaught TypeError: (intermediate value)(…) is not a function

Everything works fine when I wrote the js logic in a closure as a single js file, as:

662
Rails remote form submitting multiple times with &#39;enter&#39;

Rails remote form submitting multiple times with 'enter'

I have a form on a page that is a single input fieldI would like the user to be able to hit enter to submit the form

272