I have a multi select html field which includes categories and the elements relating to those categories
I want a person to be able to select individual courses or if they select a category (value prefixed with cat_) then it should mark the following courses as selected until it finds the next category
I have tried a few things but with no success
$(document).ready(function() {
$('#courses').change(function() {
$(this).nextUntil('option[name^="cat_"]').each().prop("selected", "selected");
$(this).nextUntil('option[name^="cat_"]').prop("selected", "selected");
$(this).closest("option").nextAll().each(function() {
$this.prop("selected", "selected");
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" name="courses[]" id="courses" multiple="true">
<option value="cat_1">data / </option>
<option value="1">data / Guitar </option>
<option value="2">data / keyboard </option>
<option value="cat_2">mandatory / </option>
<option value="cat_3">fb / </option>
<option value="3">fb / Node js </option>
<option value="4">fb / PHP programming </option>
<option value="5">fb / C# </option>
</select>