I'm trying to find a way to prevent method execution when any of 2 inputs have focus.
Both inputs should fire method on blur and prevent it when focused. Using single of these inputs works good.
The problematic situation is when blurring from input_1 and immediately focusing on input_2 (e.g. using tab) - it fires the method before input_2 gets focus.
What I would like to achieve is preventing the method execution in this case.
What I already tried:
- using flags as presented in example code -> failed as described,
- getting activeElement in fired method -> instead of getting input element, I'm getting main vue app
<div id="app" />
Example code:
<template>
<div>
<input id="input_1" @blur="changeFlagAndExecuteMethod()" @focus="focused = true"></input>
<input id="input_2" @blur="changeFlagAndExecuteMethod()" @focus="focused = true"></input>
</div>
</template>
<script>
export default {
data () {
return {
focused: false
}
},
methods: {
changeFlagAndExecuteMethod () {
this.focused = false
this.doSomething()
},
doSomething () {
// document.activeElement => results with main <div id="app" /> instead of input
if (this.focused) return
// otherwise execute method
}
}
}
</script>