11

I would like to focus on a text input in a PhoneGap application when the page loads. I have tried the MooTools version $('entry').focus(); and document.getElementById('entry').focus() when the DOM is ready.

This works fine when the HTML page is viewed in a normal web browser, but in the iPhone simulator running PhoneGap, it doesn't work.

Is there a way to focus on a form field for iPhones that forces the virtual keyboard to appear?

827
  • 4,864
  • 4
  • 19
  • 24

2 Answers2

24

As of iOS 6, the UIWebView class has a property called "keyboardDisplayRequiresUserAction". If you want to programmatically focus text form elements, set this to false via your phonegap config.xml:

<preference name="KeyboardDisplayRequiresUserAction" value="false" />

The default value is true.

More here: http://community.phonegap.com/nitobi/topics/calling_focus

adamvert
  • 615
  • 5
  • 15
6

As far as I know, this is an intentional limitation of WebKit on both iPhone and Android; only a user touch action can trigger a keyboard. I'm still trying to concoct a workaround, but I'm not very optimistic.

Luke Dennis
  • 14,212
  • 17
  • 56
  • 69
  • 2
    Did you manage to find a workaround for this? Still doesn't seem to be a solution for it. – James Oct 01 '12 at 11:32
  • @James: Never did find a workaround. However, the policy has been reversed on iOS6 for home-screen web apps and properly-configured UIWebViews. It's a good compromise, and probably the best we can hope for. – Luke Dennis Oct 03 '12 at 00:11
  • 1
    I did manage to get it working by writing a custom PhoneGap plugin and completely replace the HTML input with a native `UITextView` - not ideal, but it does work. – James Oct 03 '12 at 15:05