JQuery filter dropdown

filterjquery

Ok I have a drop down of countries and only want to display USA and Canada

I'm looking at something like this but don't think I'm doing this right

// Filter Countries
$('#selectId').filter(function() {
   return $('#option').val() == ('USA' || 'CANADA');   
});

Example HTML:

<select id="country">
  <option value="AFG">Afghanistan</option>
  <option value="ALB">Albania</option>
  <option value="ALG">Algeria</option>
  <option value="CANADA">Canada</option>
  <option value="USA">United States</option>
</select>

Best Solution

The || construct you have is invalid. Try this.

$(this).val() == 'USA' || $(this).val() == 'CANADA';  

To remove the options that don't match, you could do something like this.

$().ready( function () {
  $('#country option').filter(function() {             
     return !($(this).val() == 'USA' || $(this).val() == 'CANADA');   
  }).remove();
});