morne
morne

Reputation: 4189

search for option in dropdown box

I want to find if a option is in the dropdown and use its value. this is NOT when selected. I purely want to scan the list for Procedures and get the value from it.

<select class="FormDropDown " id="procedure-category-fld" name="procedure-category-fld">
    <option value=""></option>
    <option value="4">Cause</option>
    <option value="5">Disorder</option>
    <option value="6">Procedure</option>
</select>

Upvotes: 0

Views: 41

Answers (2)

gaetanoM
gaetanoM

Reputation: 42054

You can use .filter():

var value = $('#procedure-category-fld option').filter(function (idx, ele) {
    return ele.textContent == "Procedure";
}).val();

console.log(value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<select class="FormDropDown " id="procedure-category-fld" name="procedure-category-fld">
    <option value=""></option>
    <option value="4">Cause</option>
    <option value="5">Disorder</option>
    <option value="6">Procedure</option>
</select>

Upvotes: 3

Dekel
Dekel

Reputation: 62556

You can take the option elements from within the select you have and extract their value/text using jquery:

$(function() {
  $('#procedure-category-fld option').each(function(i, el) {
    console.log($(el).val(), $(el).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="FormDropDown " id="procedure-category-fld" name="procedure-category-fld">
    <option value=""></option>
    <option value="4">Cause</option>
    <option value="5">Disorder</option>
    <option value="6">Procedure</option>
</select>

Upvotes: 2

Related Questions