Skip to content
Advertisement

Remove options from 4 select dropdowns based on selected options in each other? (jquery allowed)

I have four selects and when one option is selected in any one dropdown, it needs to be disabled (not removed) from the other three. It should build from there so that options selected in three dropdowns shouldn’t show in the fourth. On top of that, the first dropdown will have a selected dynamic option by default on page load, which needs to be removed from the other three, even before an on.change event. I have seen several examples involving two selects but not four, and I know there has to be a simple sleek way to do this in jquery.

I’ve tried to deal with the first select separately with an on load, and the others with the code below, but the issue I have is when an on.change disable happens it ‘forgets’ about the first select option that should remain disabled in the other three. Only one option is disabled at a time. I truly apologize if this is a duplicate of an answered question.

html:

select class="custom-select uomselect" name="uom1">
  <option>None</option>
  <?php 
  foreach ($unit_list as $unitabbr) {
  echo "<option value=".$unitabbr.">".$unitabbr."</option>";
  }?>  
 </select>

<select class="custom-select uomselect" name="uom2">
  <option>None</option>
  <?php 
  foreach ($unit_list as $unitabbr) {
  echo "<option value=".$unitabbr.">".$unitabbr."</option>";
  }?>  
 </select>

<select class="custom-select uomselect" name="uom3">
  <option>None</option>
  <?php 
  foreach ($unit_list as $unitabbr) {
  echo "<option value=".$unitabbr.">".$unitabbr."</option>";
  }?>  
 </select>

<select class="custom-select uomselect" name="uom4">
  <option>None</option>
  <?php 
  foreach ($unit_list as $unitabbr) {
  echo "<option value=".$unitabbr.">".$unitabbr."</option>";
  }?>  
 </select>

This creates four selects with dynamic options from a database

Jquery: I’m trying to get the first select’s value on page load, then have on.change events for the selects so the same option value that hasn’t become the selected option in the on.change triggering dropdown will be disabled in the others.

   $(document).ready(function(){

              function getprimaryunitval() {
              return $('select[name=uom1]').val()

              }
              var primaryuom = getprimaryunitval();





 var dropdowns = $(".uomselect");
 dropdowns.change(function()
    {
 dropdowns.find('option').removeAttr('disabled', "false");
 dropdowns.find('option:not(:selected)[value="'+$(this).val()+'"]').prop('disabled', "true");
   });

});

Ideal result : all four selects will prevent the user from selecting an option twice, but also allow them to modify their choices in any order. Currently my code isn’t keeping track of all the selects properly, and options aren’t being disabled in all the selects when more than one dropdown has been used.

Thank you all.

Advertisement

Answer

Is that what you’re looking for?:

$('select').on('change', function(){
  const selectedOption = $(this).find('option:selected').val();
  $(`[value="${selectedOption}"]:not(:selected)`).attr('disabled', true)
})

var previousOption = null;
$('select').on('change', function(){
  const selectedOption = $(this).find('option:selected').val();
  $(`[value="${previousOption}"]:disabled`).attr('disabled', false);
  previousOption = null;
  $(`[value="${selectedOption}"]:not(:selected)`).attr('disabled', true);
})
$('select').on('click', function(){
  previousOption = $(this).find('option:selected').val();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select1">
  <option selected></option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option>
  <option value="option5">option5</option>
</select>
<select id="select2">
  <option selected></option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option>
  <option value="option5">option5</option>
</select>
<select id="select3">
  <option selected></option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option>
  <option value="option5">option5</option>
</select>
<select id="select4">
  <option selected></option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option>
  <option value="option5">option5</option>
</select>
Advertisement