I have multiple select elements on the page that contain the same options. I want to hide the selected options from thew other select elements.
this is an example of my code
var $drops = $('.drop'),
// store a set of options
$options = $drops.eq(1).children().clone();
$drops.change(function () {
var $other = $drops.not(this),
otherVal = $other.val(),
newVal = $(this).val(),
opts = $options.clone().filter(function () {
return this.value !== newVal;
});
$other.html($opts).val(otherVal);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="dropdown1" class="drop">
<option></option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
<option value="4">Test 4</option>
</select>
<select name="dropdown2" class="drop">
<option></option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
<option value="4">Test 4</option>
</select>
<select name="dropdown3" class="drop">
<option></option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
<option value="4">Test 4</option>
</select>
<select name="dropdown4" class="drop">
<option></option>
<option value="1">Test 1</option>
<option value="2">Test 2</option>
<option value="3">Test 3</option>
<option value="4">Test 4</option>
</select>
In this example, if I select test1 from the first select element, it must not be shown as an option in the other select elements and when test2 is selected from the second select element then that must hidden in the other select elements etc.