Is it possible using vue.js to simulate another button pressed when a button is pressed?
For example, if I press the ↓ (Arrow down) button, I would l
Sure, add a ref to tab
<div class="tab" ref="tab">
Then capture arrow click:
arrowClick() {
this.$refs.tab.click()
}
I think you're asking how to simulate a TAB keypress when the user presses DOWN with the goal of moving the focus to the next focusable input.
Instead of rewriting the key
-event, you could call HTMLElement#focus() on the next sibling:
<!-- TEMPLATE -->
<input type="text"
@keydown.up.stop.prevent="prevInput"
@keydown.down.stop.prevent="nextInput"
v-for="i in 5">
// SCRIPT
methods: {
nextInput(e) {
const next = e.currentTarget.nextElementSibling;
if (next) {
next.focus();
}
},
prevInput(e) {
const prev = e.currentTarget.previousElementSibling;
if (prev) {
prev.focus();
}
},
}
<script src="https://unpkg.com/vue@2.5.17"></script>
<div id="app">
<input type="text"
@keydown.up.stop.prevent="prevInput"
@keydown.down.stop.prevent="nextInput"
v-for="i in 5">
</div>
<script>
new Vue({
el: '#app',
methods: {
nextInput(e) {
const next = e.currentTarget.nextElementSibling;
if (next) {
next.focus();
}
},
prevInput(e) {
const prev = e.currentTarget.previousElementSibling;
if (prev) {
prev.focus();
}
},
}
})
</script>