Kokodoko
Kokodoko

Reputation: 28128

Vue vor loop index as click function argument

I want to use the index of a v-for loop as the argument for a click handler, but this returns undefined?

<div v-for="(item, key, index) in groups" v-on:click="selected(index)">{{item.name}}</div>

Handler

selected(i) {
    console.log("you clicked " + i)  // this logs "you clicked undefined"
}

Upvotes: 2

Views: 1242

Answers (1)

Bert
Bert

Reputation: 82459

Looks like you are using the syntax for objects not arrays. Change your v-for to:

<div v-for="(item, index) in groups" v-on:click="selected(index)">{{item.name}}</div>

Upvotes: 3

Related Questions