24

I've found this: Copy to Clipboard that also works on Mobile?

But VueJS doesn't use jQuery. So what is the alternative to this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ben
  • 1,550
  • 3
  • 15
  • 22

3 Answers3

54

Based on https://medium.com/vuejs-tips/tip-11-auto-select-input-text-on-focus-9eca645073cd article:

<input @focus="$event.target.select()">
Bruno De Freitas Barros
  • 2,199
  • 2
  • 17
  • 16
12
<input type="text" ref="input" @click="selectAll">

selectAll() {
  this.$refs.input.select();
}

[]: https://jsfiddle.net/s7L895n7/14/

SongZeng
  • 131
  • 1
  • 4
  • 1
    Welcome to Stack Overflow, please review: https://stackoverflow.com/help/how-to-answer – Daniel May 14 '18 at 02:17
  • I would not use the `click ` event here, because you might also want this to work when the user for example switches to the input field by tabbing; – low_rents Oct 13 '20 at 16:11
5

You can still use JQuery, just add the script to your HTML, but if you don't want to use JQuery the alternative is to use vanilla Javascript (pure JS).

The setSelectionRange(start, end) method of an input is the answer you may want.

Here's a demo.

Working demo

Gerardo
  • 979
  • 1
  • 7
  • 15