Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger an element visibility only for the clicked item in a v-for loop?

Tags:

vue.js

vuejs2

How do I make each button inside of each element to only trigger the hidden menu of the clicked item, instead of all of them?

<div v-for="(i, index) in 3">
  <v-btn @click="clicked = !clicked">Menu</v-btn>
  <p v-show="clicked">hidden menu</p>
</div>

...

data () {
  return {
    clicked: false
  }
}

Codepen: https://codepen.io/anon/pen/wmjKXB?editors=1010

What would be the best way to do it? I've been looking for an answer but couldn't find anything.

like image 528
Un1 Avatar asked Jan 28 '26 23:01

Un1


1 Answers

Use an array of objects and toggle each using the arrays index corresponding to the button.

new Vue({
  el: '#app',
     data () {
       return {
         clicked:[{ show:true}, { show:true}, { show:true}]
       }
     }
})

Here's a simple example

like image 114
Brian Lee Avatar answered Jan 31 '26 23:01

Brian Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!