Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting text in mobile Safari on iPhone

I'm trying to make it easy for an iphone user to copy some text to the clipboard in mobile safari. As in the usual "touch-hold-copy". There is a specific bit of text I want to a user to copy. I have full choice of the html markup in which to wrap the text. How can I make it easy, rather than abitrary? For instance:

  • Is there a way to "select all" the text upon touch-down using javascript? Then a user could just continue to touch-hold and then choose copy?

  • Is there a way to bring up the "select all" option? Like you can when typing in a text box? After which they can choose copy?

  • If there's no javascript solution, how can I arrange the html to help Safari select the right bit of text easily? As opposed to just a word, or a wrapping div?

I've tried onFocus="this.select()" for various elements, none seem to work. Also tried onClick.

Those who have tried to port a site that uses ZeroClipboard to the iPhone might have some ideas.

Cheers

like image 661
Goldy Avatar asked Dec 14 '09 07:12

Goldy


People also ask

How do you select text in Safari?

In the Safari app on your Mac, go to a photo or image that shows text. The text can be words, a phone number, an email address, a website address, or a street address. Move the pointer over the text, then drag to select it.

How do I search text in Mobile Safari?

Navigate to the page that you want to search. Tap the Share icon (the square with an arrow pointing out) at the bottom of the screen. Scroll past the Share options to the Action menu and select Find on Page. Type the word or phrase you're looking for in the search field and tap Search.


Video Answer


2 Answers

instead of this.select(); I used the following and it worked!

this.selectionStart=0;
this.selectionEnd=this.value.length;
like image 80
avshalom Avatar answered Sep 18 '22 11:09

avshalom


The magic sauce for me was the combination of these three:

onFocus="this.selectionStart=0; this.selectionEnd=this.value.length;" <!-- for big screens -->

onTouchEnd="this.selectionStart=0; this.selectionEnd=this.value.length;" <!-- for small screens -->

onMouseUp="return false" <!-- to stop the jitters -->
like image 30
Streeter Avatar answered Sep 22 '22 11:09

Streeter