Check if value is in select list with JQuery

Drop Down-MenuJquery Selectors

Drop Down-Menu Problem Overview


How can I, using JQuery, check if a value belongs to dropdown list or not?

Drop Down-Menu Solutions


Solution 1 - Drop Down-Menu

Use the Attribute Equals Selector

var thevalue = 'foo';
var exists = 0 != $('#select-box option[value='+thevalue+']').length;

If the option's value was set via Javascript, that will not work. In this case we can do the following:

var exists = false;
$('#select-box option').each(function(){
    if (this.value == 'bar') {
        exists = true;
        return false;
    }
});

Solution 2 - Drop Down-Menu

Just in case you (or someone else) could be interested in doing it without jQuery:

var exists = false;
for(var i = 0, opts = document.getElementById('select-box').options; i < opts.length; ++i)
   if( opts[i].value === 'bar' )
   {
      exists = true; 
      break;
   }

Solution 3 - Drop Down-Menu

Here is another similar option. In my case, I'm checking values in another box as I build a select list. I kept running into undefined values when I would compare, so I set my check this way:

if ( $("#select-box option[value='" + thevalue + "']").val() === undefined) { //do stuff }

I've no idea if this approach is more expensive.

Solution 4 - Drop Down-Menu

Why not use a filter?

var thevalue = 'foo';    
var exists = $('#select-box option').filter(function(){ return $(this).val() == thevalue; }).length;

Loose comparisons work because exists > 0 is true, exists == 0 is false, so you can just use

if(exists){
    // it is in the dropdown
}

Or combine it:

if($('#select-box option').filter(function(){ return $(this).val() == thevalue; }).length){
    // found
}

Or where each select dropdown has the select-boxes class this will give you a jquery object of the select(s) which contain the value:

var matched = $('.select-boxes option').filter(function(){ return $(this).val() == thevalue; }).parent();

Solution 5 - Drop Down-Menu

if(!$('#select-box').find("option:contains('" + thevalue  + "')").length){
//do stuff
}

Solution 6 - Drop Down-Menu

I know this is kind of an old question by this one works better.

if(!$('.dropdownName[data-dropdown="' + data["item"][i]["name"] + '"] option[value="'+data['item'][i]['id']+'"]')[0]){
  //Doesn't exist, so it isn't a repeat value being added. Go ahead and append.
  $('.dropdownName[data-dropdown="' + data["item"][i]["name"] + '"]').append(option);
}

As you can see in this example, I am searching by unique tags data-dropdown name and the value of the selected option. Of course you don't need these for these to work but I included them so that others could see you can search multi values, etc.

Solution 7 - Drop Down-Menu

if ($select.find('option[value=' + val + ']').length) {...}

Solution 8 - Drop Down-Menu

Having html collection of selects you can check if every select has option with specific value and filter those which don't match condition:

//get select collection:
let selects = $('select')
//reduce if select hasn't at least one option with value 1
.filter(function () { return [...this.children].some(el => el.value == 1) });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser271507View Question on Stackoverflow
Solution 1 - Drop Down-MenuLachlan RocheView Answer on Stackoverflow
Solution 2 - Drop Down-MenuMarco DemaioView Answer on Stackoverflow
Solution 3 - Drop Down-Menucam_pdxView Answer on Stackoverflow
Solution 4 - Drop Down-MenukeVView Answer on Stackoverflow
Solution 5 - Drop Down-MenuelaineView Answer on Stackoverflow
Solution 6 - Drop Down-MenuPhanFreeView Answer on Stackoverflow
Solution 7 - Drop Down-MenuAlexey ProkofievView Answer on Stackoverflow
Solution 8 - Drop Down-MenualanextarView Answer on Stackoverflow