This question already has an answer here:
- JQuery find index of focused input 5 answers
So I am trying to get the index of a focused input inside a parent element when clicking enter inside that input, but it doesn't work. Here is my snippet
new Vue({
el: '#container',
data: {
},
methods: {
enter(){
console.log($('[name=' + document.activeElement.name + ']').index())
}
}
})
input{display: inline-block;}
span{color: grey;font-size: 10px;display: block;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script><div id="container"><form method="POST" @keyup.enter.prevent="enter"><input type="text" placeholder="name"><span>expected input 0</span><input type="email" placeholder="email"><span>expected input 1</span><input type="password" placeholder="pass"><span>expected input 2</span></form></div>