Using Javascript/jQuery, I want compare to see if a selected value from a Select list contains a value from an array.
As you will see below, the array values are only part of the Select option value. I need to compare to see if the selected option value contains a value of 'myArr'.
My current code is:
var myArr = ["10:00", "10:20", "10:40"];
//Currently not working
$('select').on('change', function(){
if($.inArray($(this).text(), myArr)){
console.log("YES in array")
}else{
console.log("NOT in array")
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select>
<option value="43987">9:00 AM - 21 Available</option>
<option value="43988">9:20 AM - 33 Available</option>
<option value="43997">9:40 AM - 40 Available</option>
<option value="43990">10:00 AM - 10 Available</option>
<option value="43991">10:20 AM - 6 Available</option>
<option value="43992">10:40 AM - 22 Available</option>
<option value="43993">11:00 AM - 80 Available</option>
</select>
EDIT: I updated the post to not look at the value but if the 'text' is contained in the array. I don't have an option ot use the value because we post process that value as an ID to do some matching. This is why I need to know if the option 'text' is contained in the array.
inArraydoes not consider partial matches. It only considers exact matches.value="9:00"attributes on your options.