Firstly note that label
is not a valid attribute for an option
element. To store custom metadata in an element, use a data-*
attribute, eg data-label
.
If I query for the label "AAA" I should get 0, if I query for the label "BBB" I should get 1, if I query for the label "CCC" I should get 2
Given this statement, you can use the attribute selector to find the required option
and the val()
to get its value
. Try this:
var query = 'BBB';
var val = $('#geo_level_one option[data-label="' + query + '"]').val();
console.log(val);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="geo_level_one">
<option class="" value=""></option>
<option data-label="AAA" value="0">AAA</option>
<option data-label="BBB" value="1">BBB</option>
<option data-label="CCC" value="2">CCC</option>
</select>