Reputation: 12503
I have a select multiple with option groups:
<select _ngcontent-oeq-14="" class="form-control" id="category-select" multiple="">
<optgroup label="grocery products">
<option>meat</option>
<option>dairy</option>
<option>confectionary</option>
<option>dessert</option>
<option>baking</option>
<option>condiments</option>
<option>beverages</option>
<option>Dr IQ</option>
<option>Magma</option>
<option>Tornado</option>
</optgroup>
<optgroup label="meals">
<option>African</option>
<option>American</option>
<option>Argentine</option>
<option>Asian</option>
<option>Asian Fusion</option>
<option>BBQ</option>
<option>Bakery</option>
<option>Beverages</option>
<option>Brazilian</option>
<option>Breakfast</option>
<option>British</option>
<option>Cafe</option>
<option>Cambodian</option>
<option>Chinese</option>
<option>Coffee and Tea</option>
<option>Contemporary</option>
<option>Continental</option>
<option>Deli</option>
<option>Desserts</option>
<option>Drinks Only</option>
<option>European</option>
<option>Fijian</option>
<option>Filipino</option>
<option>Finger Food</option>
<option>Fish and Chips</option>
<option>French Fusion</option>
<option>German</option>
<option>Greek</option>
<option>Grill</option>
<option>Healthy Food</option>
<option>Ice Cream</option>
<option>Indian</option>
<option>Indonesian</option>
<option>International</option>
<option>Irish</option>
<option>Italian</option>
<option>Japanese</option>
<option>Jewish</option>
<option>Juices</option>
<option>Kiwi</option>
<option>Korean</option>
<option>Latin</option>
<option>American</option>
<option>Lebanese</option>
<option>Malaysian</option>
<option>Mediterranean</option>
<option>Mexican</option>
<option>Middle Eastern</option>
<option>Mongolian</option>
<option>Moroccan</option>
<option>Nepalese</option>
<option>North Indian</option>
<option>Pacific</option>
<option>Persian</option>
<option>Pizza</option>
<option>Portuguese</option>
<option>Pub Food</option>
<option>Seafood</option>
<option>Singaporean</option>
<option>South Indian</option>
<option>Spanish</option>
<option>Sri Lankan</option>
<option>Steakhouse</option>
<option>Street Food</option>
<option>Sushi</option>
<option>Taiwanese</option>
<option>Thai</option>
<option>Turkish</option>
<option>Vietnamese</option>
</optgroup>
</select>
I need to display a form control only if a particular option group (meal) has one or more options selected.
I already have this working function that executes every time the multiselect changes:
changed() {
this.selectedCategories = [];
var self = this;
$('#category-select option:selected').each(function () {
self.selectedCategories.push(
<any>($(this).text(), $(this).val())
)
});
if (this.selectedCategories.length < this.numberOfCategories) {
this.allSelected = false;
}
console.log(this.selectedCategories);
this.onCategoriesChanged.emit(true);
}
I'm trying to modify it to do the check to see if any "meals" are selected. I'm using this SO answer:
changed() {
this.selectedCategories = [];
var self = this;
$('#category-select option:selected').each(function () {
if ($(this).parent()[0].label === "meals") {
// at least one meal is selected, set the meal selected flag to true
}
self.selectedCategories.push(
<any>($(this).text(), $(this).val())
)
});
if (this.selectedCategories.length < this.numberOfCategories) {
this.allSelected = false;
}
console.log(this.selectedCategories);
this.onCategoriesChanged.emit(true);
}
But my line here isn't quite right because the if never equates to true:
$(this).parent()[0].label === "meals"
What am I doing wrong?
Upvotes: 0
Views: 461
Reputation: 18109
Use this condition to check if the parent optgroup is the mentioned one:
$(this).parent('optgroup[label="meals"]').length
instead of
$(this).parent()[0].label === "meals"
Upvotes: 1