2

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"
}
1
  • 1
    Is groups an array or an object? Commented Oct 8, 2017 at 17:27

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I didn't realise there was a difference... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.