4

I have an input for which is using the following event :

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

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

The above method copies the value. I need to trigger the above select focus event when the user clicks copy How can be it achieved correctly?

3 Answers 3

2

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 to add 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 inside the method trigger the focus programmatically :

 methods: {
      async copy(s) {
      await navigator.clipboard.writeText(s) 
      this.$refs.input.focus();
     ...
    }
}  
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answering, saved method is another method where I need to trigger it as well
please explain more the scenario
There is a method which copies the value besides the focus above method I need to trigger the above select when the user clicks copy async copy(s) { await navigator.clipboard.writeText(s) }
Please try to edit the question with these details and explain more the desired behavior
It seems working but has only one issue. I am using the same copy method for 2 inputs when I use the same ref it selects first only when I add to refs in the same method it selects only for the last one.
0

Give a ref to your input :

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

then anywhere in your Component script :

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

Comments

0

You can have the $event reference to the saved method

  <b-nput
        class="input"
        id="link"
        v-model="link"
        placeholder="link"
        @focus="saved"
      ></b-input>


methods: {
  saved(event){
    console.log(event)
  }
}

ref - https://v2.vuejs.org/v2/guide/events.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.