I have a select input box which allows me to select multiple values. In this case I have 4 categories and when I click on a category the subcategories appear. As you can see it's working just fine. The only issue is that when I select more then one category it shows the subcategories of the first selected category only. How can I show each category along with the subcategories when multiple categories are selected? For example if I select on category 1, category 2 & category 3, I want the following to show:
Category 1: Subcategory 1.1, Subcategory 1.2
Category 2: Subcategory 2.1, Subcategory 2.2
Category 3: Subcategory 3.1, Subcategory 3.2
$('select').on('change', function () {
var subcategories = '';
var str = "This category contains these subcategories: ";
var result = str.bold();
switch (this.value) {
case 'Category 1':
subcategories = "Subcategory 1.1, Subcategory 1.2";
break;
case 'Category 2':
subcategories = "Subcategory 2.1, Subcategory 2.2";
break;
case 'Category 3':
subcategories = "Subcategory 3.1, Subcategory 3.2";
break;
case 'Category 4':
subcategories = "Subcategory 4.1, Subcategory 4.2";
break;
}
document.getElementById("showSubcategory").innerHTML = result + subcategories;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select multiple>
<option value="Category 1">Category 1</option>
<option value="Category 2">Category 2</option>
<option value="Category 3">Category 3</option>
<option value="Category 4">Category 4</option>
</select>
<div id="showSubcategory"></div>