How to trigger input focus event inside method in Vue.js?
P粉702946921
P粉702946921 2023-10-29 23:56:42
0
2
562

I have an input that uses the following events:

<b-nput
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
            @focus="$event.target.select()"
          ></b-input>

How do I use this inside @focus="$event.target.select()" Event:

The above method copies the value. I need to trigger the above selection focus event when the user clicks copy How can this be done correctly?

P粉702946921
P粉702946921

reply all(2)
P粉441076405

Provide a reference for your input:

<b-input
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
            ref="theInput"
          ></b-input>

Then anywhere in the component script:

this.$refs['theInput'].focus();
P粉555696738

Add saved method as focus event handler:

@focus="saved"

method:

methods: {
  saved(event){ //the event is passed automatically as parameter
     event.target.select()
  }
}

edit:

Try adding ref to the input element

<b-input
          ref="input"
            class="input"
            id="link"
            v-model="link"
            placeholder="link"
         
            @focus="$event.target.select()"
          ></b-input>

Then trigger focus programmatically within the method:

methods: {
      async copy(s) {
      await navigator.clipboard.writeText(s) 
      this.$refs.input.focus();
     ...
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!