Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Something as simple as the jQuery .select() should just work across all browsers and platforms but it doesn't. I know jQuery Mobile attempts to solve the deficiencies that the multi-platform/device ecosystem creates, however not everyone wants to use jQM and sometimes it's easier to create a small workaround. This is exactly what I did to get input field text selection working for my Travel Microblog project.

This is what I was trying to achieve...A popover div displaying an <input type="text"> form element with a URL. I wanted to have this field focused and its text selected as soon as it was displayed.
iosinpselect.jpg


My first version of the code went something like this...
 This doesn't work on iOS
var shareInput = $.('input#shareUrl');
shareInput.focus();
shareInput.select();


That worked on all the browsers I tried it with on the desktop, but refused to select the text on my iPhone. Apparently this is a known issue, the solution relies on not using .select()! Counterintuitive isn't it!

So if .select() can't be used what then?



The trick is to go back to the non-jQuery code (I know right!) and set the selection manually. For this you set the selectionStart and selectionEnd attributes.

The code now becomes something like this...
 This works on iOS
var shareInput = $.('input#shareUrl');
shareInput.focus();
shareInput[0].selectionStart = 0;
shareInput[0].selectionEnd = shareInput.val().length;


Bit ugly, but it works.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Using DeoxIT to repair old game catridges

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity