I need to add class to label element when I focus on an input element which is below the label. My current solution is this:
HTML:
<label for="formProductId" ref="productIdLabel" class="form-element-title">Product ID</label>
<input id="formProductId" @blur="toggleFocus('productIdLabel', false)" @focus="toggleFocus('productIdLabel', true)" v-model="filterValues.productId" :name="filterOptions.productId === true ? 'productId' : false" type="text">
JS:
toggleFocus(ref: string, enable: boolean) {
if (enable) {
(this.$refs[ref] as HTMLElement).classList.add("js-focused");
} else {
(this.$refs[ref] as HTMLElement).classList.remove("js-focused");
}
}
I would like to remove the ref attribute and toggle js-focused class completely by the selected element itself. How do I select the closest label element and edit it's class?