remove values from select through the jquery-based wildcard

You have a few issues in your code:

  • You should split filteredlanguages on a regex to allow for spaces between words
  • You are comparing the value of the option (which is a date string) with the language; you should be comparing the text of the option instead.
  • You should be checking whether any of the filteredLanguages values exist in the option text; you can do this with Array.some
  • You can remove an option simply with $(this).remove()

This code will do what you want:

var filteredLanguages = $('#filteredlanguages').val().split(/\s*,\s/);
$('#Date > option').each(function(i) {
  let val = $(this).val();
  if (val == 'selection' || val == '') return;
  let text = $(this).text();
  if (!filteredLanguages.some(l => text.includes(l))) {
<script src=""></script>
<input type=text id="filteredlanguages" hidden value="Spanish, Latin">

<select name="Date" id="Date" class="form-control input-lg">
  <option value="selection">Select Date</option>
  <option value="2021-01-11T00:00:00.000Z">11 January - 12 March (English)</option>
  <option value="2021-03-22T00:00:00.000Z">22 March - 21 May (Spanish)</option>
  <option value="">I havent decided yet</option>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top