Ivan
Ivan

Reputation: 23

Select first value when filtering select box

I have a problem with the script below, in a way that if "product color" is changed (selected) more than once, the first preselected / filtered item in fruit dropdown is actually the last one from previous array list and it should be the first one on that list.

How can I force it to be always first value?

Example is below:

$(function() {
  var $product = $('[name="filter-product"]');
  var $fruits = $('[name="filter-fruits"]');

  var $fruitsList = $fruits.find('option').clone();

  var fruit = {
    "Green": ["All", "Fruit 1", "Fruit 3", "Fruit 5"],
    "Yellow": ["All", "Fruit 1", "Fruit 3", "Fruit 4", "Fruit 5"]
  }

  $product.change(function() {
    var $selectedProduct = $(this).find('option:selected').text();
    $fruits.html($fruitsList.filter(function() {
      return $.inArray($(this).text(), fruit[$selectedProduct]) >= 0;
    }));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Color</h4>
<select name="filter-product">
  <option value="All">All</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
</select>
<h4>Fruit</h4>
<select name="filter-fruits">
  <option value="All">All</option>
  <option value="fruit1">Fruit 1</option>
  <option value="fruit2">Fruit 2</option>
  <option value="fruit3">Fruit 3</option>
  <option value="fruit4">Fruit 4</option>
  <option value="fruit5">Fruit 5</option>
</select>

Upvotes: 2

Views: 264

Answers (3)

Manoj
Manoj

Reputation: 1195

$(function() {
  var $product = $('[name="filter-product"]');
  var $fruits = $('[name="filter-fruits"]');

  

  var fruit = {
    "Green": ["All", "Fruit 1", "Fruit 3", "Fruit 5"],
    "Yellow": ["All", "Fruit 1", "Fruit 3", "Fruit 4", "Fruit 5"]
  }

  $product.change(function() {
//moving this clone function inside onclick can help;
  var $fruitsList = $fruits.find('option').clone();
    var $selectedProduct = $(this).find('option:selected').text();
    $fruits.html($fruitsList.filter(function() {
      return $.inArray($(this).text(), fruit[$selectedProduct]) >= 0;
    }));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Color</h4>
<select name="filter-product">
  <option value="All">All</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
</select>
<h4>Fruit</h4>
<select name="filter-fruits">
  <option value="All">All</option>
  <option value="fruit1">Fruit 1</option>
  <option value="fruit2">Fruit 2</option>
  <option value="fruit3">Fruit 3</option>
  <option value="fruit4">Fruit 4</option>
  <option value="fruit5">Fruit 5</option>
</select>

Upvotes: 0

RAJNIK PATEL
RAJNIK PATEL

Reputation: 1049

$(function() {
  var $product = $('[name="filter-product"]');
  var $fruits = $('[name="filter-fruits"]');

  var $fruitsList = $fruits.find('option').clone();

  var fruit = {
    "Green": ["All", "Fruit 1", "Fruit 3", "Fruit 5"],
    "Yellow": ["All", "Fruit 1", "Fruit 3", "Fruit 4", "Fruit 5"]
  }

  $product.change(function() {
    var $selectedProduct = $(this).find('option:selected').text();
    $fruits.html($fruitsList.filter(function() {
      return $.inArray($(this).text(), fruit[$selectedProduct]) >= 0;
    }));
    $fruits[0].selectedIndex = 0;
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Color</h4>
<select name="filter-product">
  <option value="All">All</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
</select>
<h4>Fruit</h4>
<select name="filter-fruits">
  <option value="All">All</option>
  <option value="fruit1">Fruit 1</option>
  <option value="fruit2">Fruit 2</option>
  <option value="fruit3">Fruit 3</option>
  <option value="fruit4">Fruit 4</option>
  <option value="fruit5">Fruit 5</option>
</select>

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337743

To do this you can manually set the selectedIndex of the select back to 0 after updating the option elements, like this:

$(function() {
  var $product = $('[name="filter-product"]');
  var $fruits = $('[name="filter-fruits"]');

  var $fruitsList = $fruits.find('option').clone();

  var fruit = {
    "Green": ["All", "Fruit 1", "Fruit 3", "Fruit 5"],
    "Yellow": ["All", "Fruit 1", "Fruit 3", "Fruit 4", "Fruit 5"]
  }

  $product.change(function() {
    var $selectedProduct = $(this).find('option:selected').text();
    $fruits.html($fruitsList.filter(function() {
      return $.inArray($(this).text(), fruit[$selectedProduct]) >= 0;
    }));
    $fruits[0].selectedIndex = 0; // select the first option
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4>Color</h4>
<select name="filter-product">
  <option value="All">All</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
</select>
<h4>Fruit</h4>
<select name="filter-fruits">
  <option value="All">All</option>
  <option value="fruit1">Fruit 1</option>
  <option value="fruit2">Fruit 2</option>
  <option value="fruit3">Fruit 3</option>
  <option value="fruit4">Fruit 4</option>
  <option value="fruit5">Fruit 5</option>
</select>

Upvotes: 2

Related Questions