After some experimenting, I managed to enable text selection on iOS by utilizing
e.target.setSelectionRange(0, 999)
:
onUp(e){
e.preventDefault() // To counter the default behavior of placing the caret between characters on mouse up.
e.target.setSelectionRange(0, 999) // Select (most likely) all the text in the input field.
}
<input @mouseup="onUp" v-model="input"/>
https://codepen.io/kslstn/pen/ZvoEQa
However, this functionality is limited to trusted events - events directly caused by user interaction (https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted). My goal is to reveal a hidden Vue component when requested by the user through a click. Upon display, the content within the input field of that component should automatically be selected.
I went through several attempts:
- Setting the selection range when the component mounts (in mounted() hook). This proved ineffective as it was not triggered by a trusted event.
- Having the click event that reveals the component handle the text selection by targeting its ID. However, this method was messy and unsuccessful since the component wasn't yet in the DOM during the click event.
- Implementing a setTimeout in conjunction with the second approach. Nonetheless, the untrusted nature of the timeout event prevented the desired text selection.
Am I asking for too much?