0

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.

3
  • 1
    The values of the options do not match those in the array. inArray does not consider partial matches. It only considers exact matches. Commented Jul 29, 2020 at 17:54
  • 1
    The easiest solution would be to actually put value="9:00" attributes on your options. Commented Jul 29, 2020 at 17:54
  • @Taplar, right now I don't have that option. The values are the way they are needing. I am updating my question because a quanityte might be part of the "Available." Commented Jul 29, 2020 at 17:59

5 Answers 5

2

var myArr = ["10:00", "10:20", "10:40"];

$('select').on('change', function() {
  const time = this.value.split(' ')[0];
  
  if($.inArray(time, myArr) > -1) {
    console.log('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>9:00 AM - Available</option>
  <option>9:20 AM - Available</option>
  <option>9:40 AM - Available</option>
  <option>10:00 AM - Available</option>
  <option>10:20 AM - Available</option>
  <option>10:40 AM - Available</option>
  <option>11:00 AM - Available</option>
</select>

If you do not want to change the value, then grab the selected option, string off the leading time, and use that in your inArray call.

Edit: You actually don't need the option. Just the value.

Sign up to request clarification or add additional context in comments.

3 Comments

Works great, great solution. Simple. Thank you. Of course the split().
So, this works if the option does not have a 'value=' property. I updated my question to add a 'value=' property with various IDs. I actually need to compare the text. When I tried using the $(this).text().split(' ' )[0]; That did not work. Is there anyway to compare the text value with the array value?
That would be my previous version that used var $option = $('option:selected', this); var time = $option.text().split(' ')[0]; @ClosDesign
0

You didn't have correct value attribute that would be an exact match with those that are in array. So simply add value attribute for each options. I also used js includes() method of the array to check exactly what you tried, but with jQuery

var myArr = ["10:00", "10:20", "10:40"];

    //Currently not working

$('select').on('change', function(){
    if(myArr.includes($(this).val())){
     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="9:00">9:00 AM - Available</option>
     <option value="9:20">9:20 AM - Available</option>
     <option value="9:40">9:40 AM - Available</option>     
     <option value="10:00">10:00 AM - Available</option>
     <option value="10:20">10:20 AM - Available</option>
     <option value="10:40">10:40 AM - Available</option>
     <option value="11:00">11:00 AM - Available</option>
    </select>

3 Comments

My apologies, I updated the Post, the value is not possible because we do some post processing based on an ID that we populate with the value. I need to know if the 'text' in the option is contained in the array.
Then yeah there is an alternative way to get the selected value which was used in @Taplar answer
So I tried getting the value, but I updated my question and added values to the ID, I ended up testing @Taplar's example, which worked when I did not have a 'value=' property on the option values, but once I added the 'value=' property, Taplar's solution does not work. So I tried the jQuery .text() instead. But that returned False on all selections.
0

just try to use method includes(). Link

var myArr = ["10:00", "10:20", "10:40"];

$('select').on('change', function(){

    if(myArr.includes($(this).val())){
     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="9:00">9:00 AM - Available</option>
     <option value="9:20">9:20 AM - Available</option>
     <option value="9:40">9:40 AM - Available</option>     
     <option value="10:00">10:00 AM - Available</option>
     <option value="10:20">10:20 AM - Available</option>
     <option value="10:40">10:40 AM - Available</option>
     <option value="11:00">11:00 AM - Available</option>
    </select>

Comments

0

Please see my solution!

var myArr = ["10:00 AM - 10 Available", "10:20 AM - 6 Available", "10:40"];

    //Currently not working

$('select').on('change', function(){
    var optionSelected = $(this).find("option:selected");
    var textSelected   = optionSelected.text();

    if(myArr.includes(textSelected)){
     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>

Comments

0

how about split the value into an array of words first. then use the includes method

var myArr = ["10:00", "10:20", "10:40"];
$('select').on('change', function(){
let inArray = false
$(this).val().split(" ").forEach((element) => {
     if(myArr.includes(element)){
     inArray = true
    }
  });  
  if(inArray){
  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>9:00 AM - Available</option>
     <option>9:20 AM - Available</option>
     <option>9:40 AM - Available</option>     
     <option>10:00 AM - Available</option>
     <option>10:20 AM - Available</option>
     <option>10:40 AM - Available</option>
     <option>11:00 AM - Available</option>
    </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.