Hope the question made some sense. I'm using Vue JS and I have few elements being looped using v-for. Inside each block of elements being looped, is a button along with a div. In this case my loop runs three times. That means I get three buttons in each looped block of code, along with those three div's.
When I click on one of the three buttons I would like for the div in that block to receive a class dynamically. However, as it stands, when I click on one of the three buttons, the class is being added, but it is being added to all three looped div elements inside the loop.
How can I get the current looped div to only receive the specific class when the related button is clicked on, without the other two div's from the loop receiving it also?
Here is my code so far with what I have tried myself:
<template>
<div v-for="(todo, index) in todos" :key="index" class="relative">
<button @click="showDiv">
show div
</button>
<div :class="appear" class="absolute bg-red-500">
<p>{{ todo.text }}</p>
</div>
</div>
</template>
<script>
import {defineComponent} from 'vue'
export default defineComponent({
data() {
return {
appear: 'left-0',
todos: [
{ text: 'Learn Laravel' },
{ text: 'Learn Vue JS' },
{ text: 'Learn Tailwind CSS' },
]
}
},
methods: {
showDiv() {
this.appear = 'left-2/4';
}
}
})
</script>