I have a dropdown select of optional 'opportunities' (id="opportunities"
in the example below) which I enhance using the select2 jquery plugin, and wish to dynamically filter this list in order to reduce the possible options presented to a user using a 2nd select dropdown (id="group-select"
in the example below),
<label>
Select an opportunity<br>
<select id="opportunities" style="width:300px;" multiple="multiple" name="selected-opps" value="">
<optgroup label="Class A">
<option class="group-1" value="1a">Opportunity 1a</option>
<option class="group-2" value="2a">Opportunity 2a</option>
</optgroup>
<optgroup label="Class B">
<option class="group-1" value="1b">Opportunity 1b</option>
<option class="group-2" value="2b">Opportunity 2b</option>
</optgroup>
<optgroup label="Class C">
<option class="group-1" value="1c">Opportunity 1c</option>
<option class="group-2" value="2c">Opportunity 2c</option>
</optgroup>
</select>
</label>
<select id="group-select">
<option value="">Select a group</option>
<option value="group-1">group 1</option>
<option value="group-2">group 2</option>
</select>
</div>
<div id="select-opportunity"></div>
<script type="text/javascript">
(function( $ ) {
'use strict';
var opportunities = $('select#opportunities').select2({
dropdownParent: $('#select-opportunity')
});
})( jQuery );
</script>
I wish to be able to make a selection in the 2nd select, say 'group 1' and would like the select2 list to contain only 'group-1' items as per the option in the first select dropdown that have the grouo-1
class attribute.