luchito
luchito

Reputation: 125

JavaScript: Hide elements from a Dropdownlist execept one

i have this code:

$("#country").change(function() {
  if ($(this).data('options') === undefined) {
    $(this).data('options', $('#street option').clone());
  }
  var id = $(this).val();
  $('#street').prop('disabled', false);
  var options = $(this).data('options').filter('[class=' + id + ']');
  $('#street').html(options);
});	
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="country" id="country">
	<option  disabled selected>Select ...</option>
	<option value='1'>Country_one</option>
	<option value='2'>Country_two</option>
	<option value='3'>Country_three</option>
	<option value='4'>Country_four</option>
</select>

<select disabled class="js-select2"  name="street" id="street">
	<option disabled selected>Select a Street ...</option>
	<option class='1' value='1'>Street_one</option>
	<option class='1' value='2'>Street_two</option>
	<option class='1' value='3'>Street_three</option>
	<option class='1' value='4'>Street_four</option>
    <option class='2' value='5'>Street_five</option>
	<option class='2' value='6'>Street_six</option>
	<option class='3' value='7'>Street_seven</option>
	<option class='3' value='8'>Street_eight</option>
	<option class='3' value='9'>Street_nine</option>
	<option class='3' value='10'>Street_ten</option>
	<option>Street not found</option>
</select>

I need when change the option from country in the second dropdownlist stays the Select a Street ... option and when i select the Country_four in the seccond dropdownlist only appears the option Street not found

Upvotes: 0

Views: 45

Answers (1)

Shiny
Shiny

Reputation: 5055

Added a class default to your select option "Select a street", and added to the .filter() so it will always be selected

Added if / else to check the value selected - If it's anything but 4; Rebuild the options, and Enable the select. If it's 4; Empty the options, Add "Street not found" option, and Disable the select. After both of these, we select the first option so we're always showing the right message first

$('#country').on('change', function() {
  // Stores the intial Options into a Data-attribute
  if ($(this).data('options') === undefined) {
    $(this).data('options', $('#street option').clone());
  }

  if ($(this).val() !== '4') {
    // Fetches the Options from data, and filters for match & default option
    let id = $(this).val();
    let newOptions = $(this).data('options').filter(`[class="${id}"], [class="default"]`);
    $('#street')
      .empty()
      .append(newOptions)
      .prop('disabled', false)
  } else {
    // Creates an Option, containing only text
    let newOptions = $('<option></option>').text("Street not found");
    $('#street')
      .empty()
      .append(newOptions)
      .prop('disabled', true)
  }
  
  // Selects the first Option in the Select
  $('#street')[0].selectedIndex = 0
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="country" id="country">
  <option disabled selected>Select ...</option>
  <option value='1'>Country_one</option>
  <option value='2'>Country_two</option>
  <option value='3'>Country_three</option>
  <option value='4'>Country_four</option>
</select>

<select disabled class="js-select2" name="street" id="street">
  <option class="default" disabled selected>Select a Street ...</option>
  <option class='1' value='1'>Street_one</option>
  <option class='1' value='2'>Street_two</option>
  <option class='1' value='3'>Street_three</option>
  <option class='1' value='4'>Street_four</option>
  <option class='2' value='5'>Street_five</option>
  <option class='2' value='6'>Street_six</option>
  <option class='3' value='7'>Street_seven</option>
  <option class='3' value='8'>Street_eight</option>
  <option class='3' value='9'>Street_nine</option>
  <option class='3' value='10'>Street_ten</option>
  <option>Street not found</option>
</select>

Upvotes: 3

Related Questions