So I figured out how to make iOS select text by using e.target.setSelectionRange(0, 999)
:
onUp(e){
e.preventDefault() // By default, on mouse up the caret is placed between characters. That has to be negated.
e.target.setSelectionRange(0, 999) // Set the selection to (what is likely) all the text in the input field.
}
<input @mouseup="onUp" v-model="input"/>
https://codepen.io/kslstn/pen/ZvoEQa
However, this only works for trusted events: events that are a direct effect of user input (https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted). I want to show an initially hidden Vue component when the user requests it with a click. When it has appeared, the input field inside that component should get its content selected.
I tried the following:
- Do the setSelectionRange when the component appears (on mounted() of the component). Doesn't work, because it is not a trusted event (I think).
- Let the click event that summons my component set the selection on the component by finding its ID. It would be an ugly solution as it does not cleanly separate my components. It also does not work, because at the time of the click, the component is not in the DOM and the ID of the input field can't be found.
- Adding a setTimeout to solution 2. No success: the text selection is triggered by the untrusted timeout event.
Do I want too much?