[Vuejs]-Element focus jumping on setting it to empty

0👍

You could try simply reset focus manually using focus() method on the ref object.

You will need to add ref attribute to the input.

   <input ref="applyToAll" id="applyToAll" class="custom-control-input" type="checkbox" @change="applyToAll"/>

   <div v-for="(data,index) in users">
      <div :key="index">
          <input v-model="data.contactInfo.phoneNumber">
      </div>
   </div>

And your applyToAll method:

applyToAll() {
      if (this.appliedToAll) {
        const copiedPhone = this.users[0].contactInfo.phoneNumber;

        this.users[1].contactInfo.phoneNumber = copiedPhone;
        this.users[2].contactInfo.phoneNumber = copiedPhone;
      } else {
        this.users[1].contactInfo.phoneNumber = '';
        this.users[2].contactInfo.phoneNumber = '';
      }
      this.$refs.applyToAll.focus();
}

Leave a comment