[Vuejs]-How to reference the element id in VUE :class

1👍

Use $event.target.id

<div :class="setAreaStyle($event.target.id)" 
  @click="setFocus($event)" 
  id="CAR"
>

0👍

I guess the id you’ll use will come from the v-for item, no ? You can then use it in both places:

<div
  v-for="item in items"
  :key="item.id"
  :class="setAreaStyle(item.id)"
  :id="item.id"
  @click="setFocus($event)"
>

Side note: the name setAreaStyle is strange (seems it will do some side effects), you should only get something here, and it is the class name => getClass would make more sense.
Or :style="getStyle(item.id)" if you want to add inline CSS

Leave a comment