D
D
Dmitry2019-04-06 16:48:07
Vue.js
Dmitry, 2019-04-06 16:48:07

Vue.js How to get array object property on click?

Good day, friends.
I ask for your help in resolving the issue:
Several elements are displayed on the page through v-for, how to dynamically, by clicking on one of them, get its property, say title?
html:

<div v-for="product in products">
    <div>
        <div> {{ product.title }} </div>
        <div> {{ product.description }} </div>
        <div>
            <button @click="add">click</button>
        </div>
    </div>
</div>

js:
data: {
        products: [
            { title: 'product one', description: 'description for one product' },
            { title: 'product two', description: 'description for two product' }
        ],
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-04-06
@dmyoite

<button @click="onClick(product)">click me</button>

methods: {
  onClick(item) {
    console.log(item.title);
  },
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question