Reputation: 23824
I have a small web page, which uses the following code to set a selection by one click:
function select_element()
{
if (window.getSelection && document.createRange) {
sel = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
sel.removeAllRanges();
sel.addRange(range);
} else
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
}
}
It works in most browsers but not an an iPhone. Clicking on the "account" value selects the td
with the id "account".
But it does not work on an iPhone. The text just flashes and nothing gets selected. How to select text on the iPhone browser with JavaScript?
I tried to prevent the default mouse-up action but it does not help.
The aim is to make it easy to copy the text to the clipboard.
Upvotes: 1
Views: 474
Reputation: 995
Try adding the contentEditable property to the HTML element. Example:
<span contentEditable="true">Text</span>
On Android Lollipop this creates a problem where tapping the text will insert the caret instead of bringing up the context menu to copy, but on iOS it brings up the context menu when you tap the selected text.
Upvotes: 1