Skip to content
Advertisement

How to get array index from html element?

Hey In my Vue application I have the opportunity to create several persons:

JavaScript

If I want to add more persons, there is a button, which appends more of these person inputs:

JavaScript

},

Output of the console.log method in the console (clicked the add button 2 times):

JavaScript

Now the following problem: Let’s say for example we created 3 new persons. Now I recognize, that the second person is false and I want to delete it. When I click on the of person 2 I want to get the array index of the html element. I have already tried this, but it does not really work well:

JavaScript

Is there an other way how I could find out the index in the array of the html div I clicked?

Advertisement

Answer

Please, check Vue.js list rendering.

You can iterate through array with both element and index like this:

JavaScript

For your case:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement