My kingdom for selectable text box
Task:
Creating a cross-browser text box / input field of any type that can select its contents when clicked / clicked.
A problem that has not eluded for many years.
Problem:
When using a touch device, the tap event fires when the mouse uses the click event. On Apple devices , the tap event does not onfocus onclick or onfocus . This is not a Safari problem, since Google Chrome has the same problem on Apple devices, but NOT on Android devices. Obviously, there is a difference in how Apple devices handle click events.
The standard way to select text is simply:
$('input').focus(function () { $(this).select(); });
Other selected work around mentioned:
$('input').focus(function () { this.select();
Both work well, but do not work on apple devices.
The only working solution I found is to create a selection range according to this post. This is pure javascript and works great.
$('input').click(function () { document.getElementById('ID').selectionStart = 0 document.getElementById('ID').selectionEnd = 999 });
MASTER SOLUTION
With all this knowledge, I came up with my own solution.
$('.Select').focus(function () { this.select(); this.setSelectionRange(0, 9999); }).mouseup(function (e) { e.preventDefault(); });
RETURN OF THE PROBLEM
This was a long-standing problem that I thought was resolved, but now we have reached 2016, and the problem rises from the ground like the walking dead.
The specification now clearly states:
You cannot use 'setSelectionRange' in 'HTMLInputElement': The input element type ('number') does not support selection.
Thus, the master code will no longer work with the numeric fields of apple devices.
Here we are again and it’s time to get bounty hunters on the hunt!
IMPORTANT ADDITIONAL INFORMATION: I hate the apple.
$('.Select').focus(function () { this.select(); this.setSelectionRange(0, 9999); }).mouseup(function (e) { e.preventDefault(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Input type text <br/><input type="text" value="click me" class="Select"> <br/>Input type number <br/><input type="number" value="0" class="Select"> <br/>I dont like apples.